如何在asp.net Web API中返回JSON错误信息? [英] how to return json error msg in asp.net web api?

查看:304
本文介绍了如何在asp.net Web API中返回JSON错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回一个json错误消息,但目前在提琴手中,我无法在json面板中看到此消息:

I would like to return a json errormessage but at the moment in fiddler I cannot see this in the json panel:

string error = "An error just happened";
JsonResult jsonResult = new JsonResult
{
     Data = error,
     JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
response = Request.CreateResponse(HttpStatusCode.BadRequest, jsonResult.Data);

该怎么做?

推荐答案

几点:

如果您要做的只是返回包含简单错误消息的错误响应,则Web API为此提供了CreateErrorResponse方法.因此,您只需执行以下操作即可:

If all you're looking to do is return an error response containing a simple error message, Web API provides a CreateErrorResponse method for that. So you can simply do:

return Request.CreateErrorResponse(HttpStatusCode.BadRequest, 
                                   "An error just happened");

这将导致以下HTTP响应(为简洁起见,省略了其他标头):

This will result in the following HTTP response (other headers omitted for brevity):

HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 36

{"Message":"An error just happened"}

如果要返回一个自定义对象,则可以像以前一样使用Request.CreateResponse,但不要使用MVC JsonResult.相反,只需将您的对象直接传递到CreateResponse:

If you want to return a custom object instead, then you use Request.CreateResponse like you were doing, but don't use the MVC JsonResult. Instead, just pass your object directly to CreateResponse:

var myError = new
{
    Data = "An error just happened",
    OtherDetails = "foo bar baz"
};

return Request.CreateResponse(HttpStatusCode.BadRequest, myError);

现在,说您正在执行此操作,但是您没有从服务器获取JSON.重要的是要意识到,Web API通常使用内容类型协商来确定将响应发回时使用的格式.这意味着,它将查看客户端随请求发送的Accept标头.例如,如果Accept标头包含application/xml,则Web API将返回XML.如果标头包含application/json,则它将返回JSON.因此,您应该检查客户端是否发送了正确的Accept标头.

Now, say you are doing this but you're not getting JSON back from the server. It is important to realize that Web API normally uses content type negotiation to determine what format to use when sending the response back. That means, it looks at the Accept header that was sent by the client with the request. If the Accept header contains application/xml, for example, then Web API will return XML. If the header contains application/json then it will return JSON. So, you should check that your client is sending the correct Accept header.

也就是说,如果您确实要这样做,可以通过多种方法强制Web API始终以特定格式返回数据.您可以在方法级别使用CreateResponse的不同重载来完成此操作,该重载还指定了内容类型:

That said, there are ways to force Web API to always return data in a specific format if that is what you really want. You can do this at the method level by using a different overload of CreateResponse which also specifies the content type:

return Request.CreateResponse(HttpStatusCode.BadRequest, myError, 
    new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));

或者,您可以从WebApiConfig文件中的配置中完全删除XML格式化程序:

Alternatively, you can remove the XML formatter from the configuration altogether in your WebApiConfig file:

config.Formatters.Remove(config.Formatters.XmlFormatter);

这将强制Web API始终使用JSON,无论客户端要求什么.

This will force Web API to always use JSON regardless of what the client asks for.

这篇关于如何在asp.net Web API中返回JSON错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆