简体   繁体   English

HttpResponseMessage的内容为JSON

[英]Content of HttpResponseMessage as JSON

I have an ASP.NET MVC WEB API. 我有一个ASP.NET MVC WEB API。 For several reasons (redirect because of no authorizations ..), I can't just use a simple object and return it in my controller method. 由于几个原因(由于没有授权重定向..),我不能只使用一个简单的对象并在我的控制器方法中返回它。 Therefore I need the HttpResponseMessage class which allows me to redirect. 因此我需要HttpResponseMessage类,它允许我重定向。

Currently I'm doing this: 目前我这样做:

var response = new Response { responseCode = Response.ResponseCodes.ItemNotFound };
var formatter = new JsonMediaTypeFormatter();
response.Content = new ObjectContent<Response>(response, formatter, "application/json");

.. to get the object, serialized as JSON, into the content of HttpResponseMessage. ..将序列化为JSON的对象放入HttpResponseMessage的内容中。 Somehow, I have the feeling that there is another, better, way to do this. 不知何故,我觉得还有另一种更好的方法。 Any ideas on that? 有什么想法吗?

You can do: 你可以做:

var response = new Response { responseCode = Response.ResponseCodes.ItemNotFound };
Request.CreateResponse<Response>(HttpStatusCode.OK, response);

By default, Web API will set the format of the response based on the Content-Type specified in the HTTP request header but there are some overloads on the CreateResponse method where you can specify the type formatter. 默认情况下,Web API将根据HTTP请求标头中指定的Content-Type设置响应的格式,但CreateResponse方法中存在一些重载,您可以在其中指定类型格式化程序。

You can also remove the Web API XML serializer to force all responses to be JSON if that's what you want - off the top of my head I think it's a Formatters.Remove method on HttpConfiguration. 您还可以删除Web API XML序列化程序以强制所有响应为JSON(如果这是您想要的) - 我认为它是HttpConfiguration上的Formatters.Remove方法。

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

相关问题 将HttpResponseMessage内容设置为字符串和JSON文件之间是否有性能差异? - Is there any performance difference between setting HttpResponseMessage content as string and as JSON file? 将内容放在 HttpResponseMessage 对象中? - Put content in HttpResponseMessage object? HttpResponseMessage.Content为null - HttpResponseMessage.Content is null 读取和编辑HttpResponseMessage的内容 - Read and edit the content of an HttpResponseMessage 将 HttpResponseMessage 的内容转换为对象 - Converting the content of HttpResponseMessage to object HttpResponseMessage.Content.ReadAsStreamAsync() 与 HttpResponseMessage.Content.ReadAsStringAsync() - HttpResponseMessage.Content.ReadAsStreamAsync() vs HttpResponseMessage.Content.ReadAsStringAsync() 从 HttpResponseMessage 获取内容/消息 - Getting content/message from HttpResponseMessage 当来自CreateErrorResponse时,HttpResponseMessage.Content.ReadAsStringAsync不反序列化JSON - HttpResponseMessage.Content.ReadAsStringAsync don't deserialize JSON when it come from CreateErrorResponse 读取 HttpResponseMessage.Content 在读取 webapi 2 令牌时抛出 Newtonsoft.Json.JsonReaderException - Reading HttpResponseMessage.Content throws Newtonsoft.Json.JsonReaderException when reading webapi 2 token 如何将 HttpResponseMessage 内容读取为文本 - How to read HttpResponseMessage content as text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM