0
votes

I'm relatively new to Razor pages and have a question about posting to a controller action handler from a form on a Razor page. (It's in a file upload section, but I think I'm having more trouble with the route than file upload...?)

I have something like:

<form action="/UploadTheFile/Upload" ... method="post">...</form>

And I have a controller class (created with Visual Studio add new controller) that looks something like this:

    ...
namespace Controllers
{
   public class UploadTheFileController : Controller
   {
      [HttpPost]
      public async Task<IActionResult> Upload(IFormFile file)
      {
         ...
      }
   }
}

When the form submits (using fiddler, I see the post), I get a 404 not found error.

What am I doing wrong? How do I tell Razor/asp.net-core-2.1 the route?

Thanks, Owen

1

1 Answers

0
votes

After some more googling (and finding a Microsoft document that actually helped: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.0), the solution is to change one line in the Startup.cs file from:

app.UseMvc();

to:

app.UseMvcWithDefaultRoute();

Now the routing works and my Upload task is called from the form post!

--Owen