Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to get 405 (Method Not Allowed) instead of 404

I am trying to get 405 errors when a valid route is supplied but the HTTP method is not found. Currently, the application returns 404s as it requires both the route and method to match on the function (expected behaviour in MVC).

[HttpGet("api/action")]
public IActionResult ActionGet()
{
    // code
}

[HttpPost("api/action")]
public IActionResult ActionPost()
{
    //code
}

In this example, if I do a DELETE or PUT request it won't route to either of those functions and just return a 404.

My current solution is to create a function in every controller which has all the routes hardcoded to catch the request no matter what HTTP method is used. This will then just throw a 405 error.

[Route("api/action", Order = 2)]
public IActionResult Handle405()
{
    return StatusCode(405);
}

However, I don't really like this way too much as it duplicates the code over several controllers and the hardcoded route list needs to be updated every time a new action is created in the controller.

Is there a cleaner solution available to handle the routes in the way I want? Such as using attributes or filters?

like image 792
Tom Dee Avatar asked Apr 19 '19 09:04

Tom Dee


People also ask

How do I fix HTTP method not supported by this URL?

Solution. 1) You do not have a valid doGet() method, when you type the servlet's path in address bar directly, the web container like Tomcat will try to invoke the doGet() method. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ .... }

What does 405 Forbidden mean?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

What is http 405 method not allowed?

HTTP 405 Method Not Allowed error mainly occurs when the web server’s configuration prohibits performing certain actions for a specific URL. This is also considered an HTTP response status code, indicating that the server knows the request method, but the target resources do not support it. Therefore, it is a client-side error.

What does 405 mean on a website?

405 Method Not Allowed A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

Why do I get 405 r esponse when I request get?

Therefore, it makes no sense for the server to accept a GET request at that resource/URL, so it may respond with a 405 Method Not Allowed code. Since the 405 r esponse is a client error response code, it’s best to start troubleshooting any potential client-side issues.

What is the response code for r=405?

As you can probably see, there’s a flag at the end of the rule marked R=405. This explicitly states that the response code should be 405, indicating to the user that the resource exists, but the provided HTTP method was not allowed.


1 Answers

Since ASP.NET Core 2.2, the MVC services support your desired behavior by default. Make sure that the compatibility version of the MVC services is set to Version_2_2 within the ConfigureServices method.

Startup.cs

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

Example

For demonstration purposes, I have created an API controller similar to yours.

ActionsController.cs

[Route("api/[controller]")]
[ApiController]
public class ActionsController : ControllerBase
{
    [HttpGet("action")]
    public IActionResult ActionGet()
    {
        return Ok("ActionGet");
    }

    [HttpPost("action")]
    public IActionResult ActionPost()
    {
        return Ok("ActionPost");
    }
}

GET Request

GET /api/actions/action HTTP/1.1
Host: localhost:44338

200 ActionGet

POST Request

POST /api/actions/action HTTP/1.1
Host: localhost:44338

200 ActionPost

PUT Request

PUT /api/actions/action HTTP/1.1
Host: localhost:44338

405 Method Not Allowed

like image 79
prd Avatar answered Sep 21 '22 04:09

prd