title: ASP.NET Core file upload form binding problems
body: <p>I try to bind my view's form to upload method of controller, using tons of tutorials, for example:</p>
  
  <ul>
  <li><p><a href="https://docs.microsoft.com/ru-ru/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2" rel="nofollow noreferrer">https://docs.microsoft.com/ru-ru/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2</a></p></li>
  <li><p><a href="https://metanit.com/sharp/aspnet5/21.3.php" rel="nofollow noreferrer">https://metanit.com/sharp/aspnet5/21.3.php</a></p></li>
  <li><a href="https://www.c-sharpcorner.com/article/upload-download-files-in-asp-net-core-2-0/" rel="nofollow noreferrer">https://www.c-sharpcorner.com/article/upload-download-files-in-asp-net-core-2-0/</a></li>
  </ul>
  
  <p>So, as I understand, if I create a controller and view, all should work.
  The controller: </p>
  
  <p>(async is disabled here, but in both cases result is the same)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  
  // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  
  namespace ExcelParser.Controllers
  {
      public class HomeController : Controller
      {
          static HttpClient httpClient = new HttpClient();
  
          // GET: /&lt;controller&gt;/
          public IActionResult Index()
          {
              if (httpClient.BaseAddress == null)
              {
                  httpClient.BaseAddress = new Uri("http://localhost:51313");
              }
              var response = httpClient.GetAsync("api/file").Result;
              ViewBag.Templates = response.Content.ReadAsAsync&lt;IEnumerable&lt;string&gt;&gt;().Result;
  
              return View();
          }
  
          public IActionResult AddFile()
          {
              return View();
          }
  
          /*[HttpPost]
          public async Task&lt;IActionResult&gt; UploadFile(IFormFile file)
          {
              Console.WriteLine("======UPLOAD======");
              Console.WriteLine(file.FileName);
              string msg = "i go there";
              return RedirectToAction("Index");
          }*/
  
          [HttpPost]
          public IActionResult UploadFile(IFormFile file)
          {
              Console.WriteLine("======UPLOAD======");
              Console.WriteLine(file.FileName);
              string msg = "i go there";
              return RedirectToAction("Index");
          }
  
      }
  }
  </code></pre>
  
  <p>The view AddFile.cshtml:</p>
  
  <pre><code>@{
      Layout = null;
  }
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;AddFile&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form asp-controller="Home" asp-action="UploadFile" method="post"
            enctype="multipart/form-data"&gt;
  
          &lt;input type="file" name="file" /&gt;
          &lt;button type="submit"&gt;Upload File&lt;/button&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So, I place breakpoint in controller's <code>string msg = "i go there";</code> and go to webpage <a href="http://myhost:port/addfile" rel="nofollow noreferrer">http://myhost:port/addfile</a>. So, the view AddFile is rendered, but when I select a file and push the submit button, nothing happens. So, the problem that my controller doesn't receive a call for UploadFile action. I tried many examples and tutorials, and the result is the same - binding doesn't work.
  I use ASP.NET Core 2.2</p>
  
  <hr>
  
  <p>Checked html code in browser (Chrome 72 for Win). I expected  element attributes will be transformed by Razor. The code from browser:</p>
  
  <pre><code>&lt;html&gt;&lt;head&gt;
      &lt;meta name="viewport" content="width=device-width"&gt;
      &lt;title&gt;AddFile&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form asp-controller="Home" asp-action="UploadFile" method="post" enctype="multipart/form-data"&gt;
  
          &lt;input type="file" name="file"&gt;
          &lt;button type="submit"&gt;Upload File&lt;/button&gt;
      &lt;/form&gt;
  
  
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>So, it seems Razor doesn't work, and the async method UploadFile is not fired.</p>
  
  <pre><code>[HttpPost]
          public async Task&lt;IActionResult&gt; UploadFile(IFormFile file)
          {
              Console.WriteLine("======UPLOAD======");
              Console.WriteLine(file.FileName);
              await Task.Run(() =&gt;
              {
                  Thread.Sleep(2000);
                  Console.WriteLine("======ASYNC======");
              });
              string msg = "i go there";
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p>So, maybe I lost some references? This is my .cspjoj file:</p>
  
  <pre><code>&lt;Project Sdk="Microsoft.NET.Sdk.Web"&gt;
  
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netcoreapp2.2&lt;/TargetFramework&gt;
      &lt;UserSecretsId&gt;bf953a4d-5fe0-4538-aeb5-27f9b4437de6&lt;/UserSecretsId&gt;
    &lt;/PropertyGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;PackageReference Include="EPPlus" Version="4.5.3.1" /&gt;
      &lt;PackageReference Include="Microsoft.AspNetCore.App" /&gt;
      &lt;PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /&gt;
      &lt;PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" /&gt;
      &lt;PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" /&gt;
    &lt;/ItemGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;Folder Include="wwwroot\Upload\" /&gt;
    &lt;/ItemGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;None Include="wwwroot\Templates\test.xlsx" /&gt;
    &lt;/ItemGroup&gt;
  
  &lt;/Project&gt;
  </code></pre>
  
  <hr>
  
  <p>When I push submit button, a watch REST queries in the Network (Dev Tools) tab, it runs.... addfile POST query with URL <a href="https://localhost:44351/home/addfile" rel="nofollow noreferrer">https://localhost:44351/home/addfile</a>. But why it happens, I can't found.</p>
  
  <p>UPD0. Added and tried [HttpPost] method attribute. No result</p>
  
  <p>UPD1. Added html from browser and references from csproj file.</p>