I am writing an ASP.net web API controller. I have an endpoint where I only support POSTs. I am writing error handling code to return HTTP 405 (rather than the default 404) for unsupported HTTP methods.
[HttpPost]
[ODataRoute("foobar")]
public async Task<IActionResult> Post()
{
return NoContent();
}
For the remaining verbs, I have an error handling method:
[HttpGet]
[HttpPatch]
[HttpDelete]
[HttpPut]
[ODataRoute("foobar")]
public async Task<IActionResult> NotSupported()
{
// return http 405;
}
I have been making calls in Postman to test the functionality of this endpoint. With GET, the endpoint returns 201 as expected. However, only some of the other HTTP verbs are returning their expected responses. These calls are all being made with the exact same URLs, so I am baffled as to why only some verbs are reaching my NotSupported
method.
you don't need to create a special controller action. try this
[ODataRoute("foobar")]
public async Task<IActionResult> Post()
{
if( HttpContext.Request.Method == HttpMethod.Post.Method) return NoContent();
return StatusCode(405);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.