简体   繁体   English

Web API2中返回错误

[英]Return Error in Web API2

I need to return error from my Web API. 我需要从我的Web API返回错误。 That means after validation. 这意味着经过验证。 I have custom error model will send as a json. 我有自定义错误模型将作为json发送。 How can I send this? 我该如何发送? Like BadRequest(Model); 像BadRequest(Model); IHTTPActionResult is using. IHTTPActionResult正在使用。 If using HttpresponseMessage, We can pass. 如果使用HttpresponseMessage,我们可以通过。 But in IHTTPActionResult how Can I pass custom error model with BadRequest. 但是在IHTTPActionResult中,如何通过BadRequest传递自定义错误模型。

Just return an internal server error. 只需返回内部服务器错误。 It has an overload to return an exception with a message. 它有重载以返回带有消息的异常。

public IHttpActionResult YourMethod()
{
    try
    {

    }
    catch
    {
        return InternalServerError();
        //or
        return this.BadRequest(JsonConvert.SerializeObject(yourModel));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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