ASP.NET Web API - return CLR object or HttpResponseMessage

前端 未结 2 725
离开以前
离开以前 2021-02-01 22:54

What seems to be general practice in Web API for return types from action methods?

Returning CLR objects like so:

public IEnumerable          


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 23:20

    I think the first option is the best. In any case if there is no error the return code will be 200 without any setting on your side.

    If there is any exception you could throw a HttpResponseException with the appropiate code and message

    throw new HttpResponseException(
        Request.CreateResponse(HttpStatusCode.BadRequest, 'Your message'))
    

提交回复
热议问题