简体   繁体   中英

POST Method Always Returns 405 Response

I have a .NET Core 3.1 API that has two endpoints I am using to test some error handling middleware that I am writing. I am using Swagger UI to test this API.

The issue I'm having is that if any kind of exception is thrown during the execution of my POST request, it will be returned back as a 405 response and not even trigger the error handling middleware I am writing. This happens for ALL POST request across my API, not just the test one I am showing.

I decided to try an equally simple GET method and to my surprise, when an exception was thrown within it, it would trigger my error handling middleware which would then write a 500 response back to the caller. Here is the whole controller code:

[Route("api/[controller]")]
[ApiController]

public class TestingController : ControllerBase
{
    [HttpPost]
    [Route("TestPost")]
    public IActionResult TestPost([FromBody] string test)
    {
        throw new Exception();            
    }

    [HttpGet]
    [Route("TestGet/{test}")]
    public IActionResult TestGet(string test)
    {
        throw new Exception();
    }

}

So why would a POST method be returning a 405 resposne when an exception occurs, but a GET method responds with a 500 response?

EDIT: Here is the example POST request I am making

Request URL: https://localhost:44387/api/Testing/TestPost

Request Body(JSON): "test"

Here are the response headers I receive from the POST:

allow: GET cache-control: no-cache date: Wed, 19 Feb 2020 17:58:22 GMT expires: -1 pragma: no-cache server: Microsoft-IIS/10.0 status: 405 x-powered-by: ASP.NET

So I eventually figured this out with some help from Rahul Sharma . The issue did not have anything to do with the POST method, but rather the custom error handling controller that the POST request was trying to be routed to. The method in the error controller was defined as the following:

    [Route("/Error-descriptive")]
    [HttpGet]
    public IActionResult ErrorDescriptive()
    {
        if (!_env.IsNonProductionEnv())            
            throw new InvalidOperationException("This endpoint cannot be invoked in a production environment.");

        IExceptionHandlerFeature exFeature = HttpContext.Features.Get<IExceptionHandlerFeature>();
        Exception ex = exFeature.Error;

        return Problem(detail: ex.StackTrace, title: ex.Message);
    }

The issue was the [HttpGet] attribute used on this method, this was preventing my POST request from being routed to this controller when the exception occurred, which in turn produced a 405 response. Removing the attribute allows the error handler code to run, and produce a 500 response.

I apologize for not adding this code initially, however I was pretty confident that this code was not a cause of the issue because the code in the error controller wasn't being executed at all.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM