How do you throw HttpResponseException in ASP 5 (vnext)

前端 未结 1 470
故里飘歌
故里飘歌 2021-02-13 19:19

I\'m writing an api controller in ASP 5. I want to return a bad request code exception if the parameters passed to the service are incorrect. In the current version of webapi I

相关标签:
1条回答
  • To answer your question, you can use the WebApiCompatibilityShim which ports HttpResponseException (and many other features) forward into MVC 6. (thanks to DWright for the link to that article.)

    It seems like the MVC-6 way to do it is to return an IActionResponse from your method and then call HttpBadRequest() which is a method on the base Microsoft.AspNet.Mvc.Controller class that returns an BadRequestResult, which I believe is the best way to get a 400 status into the response.

    The Controller class has methods for other response codes as well - including the HttpNotFound() method which causes a 404 to be returned.

    See also: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api

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