404 error when making a POST call from Angular HttpClient to ASP.NET Core 2.2 Web API (with CORS enabled)

前端 未结 2 1747
星月不相逢
星月不相逢 2021-01-27 21:23

I\'ve written a simple ASP.NET Core 2.2 Web API. The POST method always returns a 404, but GET requests succeed.

public cl         


        
相关标签:
2条回答
  • 2021-01-27 21:38

    You have not specified a route to the action. You can either change your post to go to /api/Test or set the attributes on the action as follows:

    [HttpPost("Create")]
    public async Task<ActionResult<string>> Create([FromBody] TestPayload value)
    {
        return Ok("");
    }
    

    or

    [HttpPost]
    [Route("Create")]
    public async Task<ActionResult<string>> Create([FromBody] TestPayload value)
    {
        return Ok("");
    }
    

    or update the controller route to include the action

    [Route("api/[controller]/[action]")]
    
    0 讨论(0)
  • 2021-01-27 21:44

    Try to add:

    [EnableCors(origins: " * ", headers: " * ", methods: " * ")]
    

    to your Controller class. ( remove with spaces in the quotes )

    More info on Microsoft-Website

    0 讨论(0)
提交回复
热议问题