使用更新的ASP。NET Web API,在Chrome中我看到XML -我如何将其更改为请求JSON,以便我可以在浏览器中查看它?我相信这只是请求头的一部分,我是正确的吗?
当前回答
这段代码使json成为我的默认格式,并允许我使用XML格式。我将附加xml=true。
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("xml", "true", "application/xml"));
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
谢谢大家!
其他回答
WebApiConfig是你可以配置你想要输出json还是xml的地方。缺省情况下为xml格式。在register函数中,我们可以使用HttpConfiguration Formatters来格式化输出。
System.Net.Http.Headers => MediaTypeHeaderValue("text/html")需要获得json格式的输出。
当User-Agent头包含“Chrome”时,我使用全局动作过滤器删除Accept: application/xml:
internal class RemoveXmlForGoogleChromeFilter : IActionFilter
{
public bool AllowMultiple
{
get { return false; }
}
public async Task<HttpResponseMessage> ExecuteActionFilterAsync(
HttpActionContext actionContext,
CancellationToken cancellationToken,
Func<Task<HttpResponseMessage>> continuation)
{
var userAgent = actionContext.Request.Headers.UserAgent.ToString();
if (userAgent.Contains("Chrome"))
{
var acceptHeaders = actionContext.Request.Headers.Accept;
var header =
acceptHeaders.SingleOrDefault(
x => x.MediaType.Contains("application/xml"));
acceptHeaders.Remove(header);
}
return await continuation();
}
}
似乎有用。
我不清楚为什么答案会这么复杂。当然,有很多方法可以做到这一点,QueryStrings,头和选项…但我认为最好的做法很简单。你请求一个简单的URL(例如:http://yourstartup.com/api/cars),作为回报,你得到JSON。你得到JSON和适当的响应头:
Content-Type: application/json
在寻找这个问题的答案时,我发现了这个帖子,并不得不继续下去,因为这个公认的答案并不完全正确。我确实找到了一个答案,我觉得它太简单了,不是最好的答案:
设置默认的WebAPI格式化程序
我把我的小费也加在这里。
WebApiConfig.cs
namespace com.yourstartup
{
using ...;
using System.Net.Http.Formatting;
...
config.Formatters.Clear(); //because there are defaults of XML..
config.Formatters.Add(new JsonMediaTypeFormatter());
}
我确实有一个问题,默认(至少我看到的那些)来自哪里。它们是. net默认的,还是在其他地方创建的(由我项目中的其他人创建)。无论如何,希望这能有所帮助。
这里有一个类似于杰森的解决方案。ceneno 's和其他答案,但使用System.Net.Http.Formatting的内置扩展。
public static void Register(HttpConfiguration config)
{
// add support for the 'format' query param
// cref: http://blogs.msdn.com/b/hongyes/archive/2012/09/02/support-format-in-asp-net-web-api.aspx
config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
// ... additional configuration
}
在WebApi的早期版本中,该解决方案主要用于支持OData的$格式,但它也适用于非OData实现,并返回 内容类型:application / json;Charset =utf-8报头。
它允许您在使用浏览器测试时将&$format=json或&$format=xml附加到uri的末尾。在使用非浏览器客户端(可以设置自己的头文件)时,它不会干扰其他预期行为。
一个快速的选择是使用MediaTypeMapping专门化。下面是一个在Application_Start事件中使用QueryStringMapping的例子:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("a", "b", "application/json"));
现在,只要url包含querystring(在这种情况下,a=b), Json响应就会显示在浏览器中。
推荐文章
- 如何合并2 JSON对象从2个文件使用jq?
- 如何禁用谷歌翻译从HTML在Chrome
- 在VS Code中禁用“Comments are not allowed In JSON”错误
- 如何在Kotlin解析JSON ?
- JSON文件的蒙古导入
- Chrome开发工具:如何找出什么是覆盖CSS规则?
- 如何用jQuery / JavaScript解析JSON数据?
- jQuery在请求体中发布有效的json
- 将JSON显示为HTML
- 在JSON键名中哪些字符是有效的/无效的?
- 如何JSON序列化集?
- 如何检查JSON键是否存在?
- 返回内容与IHttpActionResult非ok响应
- 资源解释为样式表,但以MIME类型text/html传输(似乎与web服务器无关)
- Chrome iOS(和Safari)的远程调试