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?
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{ .... }
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.
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.
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.
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.
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.
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With