WebApi2 IHttpActionResult strongly typed return values

后端 未结 4 1428
借酒劲吻你
借酒劲吻你 2021-01-18 16:10

It is possible and not ideal to do this: (a vastly simplified example!)

[Serializable]
public class MyRecord
{
    public string key {get; set;}
    public          


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 16:30

    The problem with using strongly typed method signatures is that there is no way for you to return an "error" object if something is wrong with the request, be it validation errors or not found errors.

    So if you want to use strongly typed signatures then you either have to included some sort of "error" object reference in your return type or explicitly throw exceptions..

    The good thing about using HttpActionResult is that you are not constrained to a specific return type. So you could, for example, return OK(someObject) or BadRequest(errorObject). However, it is up to the developer to write the method correctly and double check that nothing happens like the example you mentioned above.

提交回复
热议问题