Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Razor page post action throws HTTP 405 error

I have an extremely simple Razor test page, but the post action throws an HTTP 405 error, and the code in the OnPostAsync method never executes, which is required for further processing.

@page
@model SubmitTestModel

<div>
    <h2>
        Submit test
    </h2>
</div>

<form method="post">
    <input type="submit" value="Submit" />
</form>

The Page model consists of nothing more than

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Threading.Tasks;

namespace Test.Pages
{
    public class SubmitTestModel : PageModel
    {
        public SubmitTestModel()
        { }

        [BindProperty]
        public IFormFile UploadFile { get; set; }

        public IActionResult OnGetAsync()
        {
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            return Page();
        }
    }
}
like image 442
Mike Avatar asked Oct 25 '25 05:10

Mike


1 Answers

A 405 Method Not Allowed indicates that the endpoint is not configured to accept a POST in this case. That is why your OnPostAsync() method is not being executed, the request was never routed to that endpoint.

For Razor pages (Non MVC), the convention for POST is that you need to include an action or endpoint name, we use the asp-page-handler="MyAction" attribute to specify the method that will handle the request, by convention the method name should be in the format OnPostMyActionAsync for an async handler.

So let's change your form:

<form asp-page-handler="Upload" method="post">
    <!-- Put a file Upload control here ;) -->
    <input type="submit" value="Submit" />
</form>

You could also have placed this attribute on the submit button, this allows support for multiple handlers for the same form that will provide different actions:

<form method="post">
    <!-- Put a file Upload control here ;) -->
    <input asp-page-handler="Upload" type="submit" value="Submit" />
</form>

Then in the code behind, change your handler to this:

public async Task<IActionResult> OnPostUploadAsync()
{
    return Page();
}

It is common to use a verb that describes the overall intent for the method handler, things like Add, Delete, Update, then append Item or the record type if this is an action on a specific item on a page that shows many items.

Have a read over File upload Scenarios in ASP.NET Core 7.0 for other information related to both posting to form action handlers and how to use the file input form field.

  • You could also try adding the [HttpPost] attribute to your endpoint, that might work around the issue in Razor Pages but it is simpler to understand and stick to the conventions, rather than subvert them.

Your code is however correct for an MVC application, although you haven't hookedup the file upload control yet, but so far its good. If I post that code you have directly into a blank MVC project (.Net 7) then it handles both the GET and POST requests as expected.

like image 165
Chris Schaller Avatar answered Oct 27 '25 01:10

Chris Schaller