提问人:naspinski 提问时间:3/24/2012 最后编辑:DanielVnaspinski 更新时间:4/9/2022 访问量:974912
如何使用 Chrome 让 Web API ASP.NET 返回 JSON 而不是 XML?
How do I get ASP.NET Web API to return JSON instead of XML using Chrome?
问:
使用较新的 ASP.NET Web API,在 Chrome 中我看到 XML - 如何将其更改为请求 JSON,以便我可以在浏览器中查看它?我确实相信这只是请求标头的一部分,我这样做正确吗?
答:
查看 WebAPI 中的内容协商。这些(第 1 部分和第 2 部分)非常详细和透彻的博客文章解释了它是如何工作的。
简而言之,你是对的,只需要设置 or 请求标头。假设您的 Action 未编码为返回特定格式,您可以设置 .Accept
Content-Type
Accept: application/json
评论
一个快速选项是使用 MediaTypeMapping 专用化。下面是在 Application_Start 事件中使用 QueryStringMapping 的示例:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("a", "b", "application/json"));
现在,在这种情况下,每当 url 包含查询字符串 ?a=b 时,Json 响应就会显示在浏览器中。
评论
我发现 Chrome 应用程序“高级 REST 客户端”非常适合与 REST 服务一起使用。您可以将 Content-Type 设置为以下项: 高级 REST 客户端application/json
MVC4 快速提示 #3 – 从 Web API 中删除 XML 格式化程序 ASP.Net
在添加行中:Global.asax
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
这样:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}
评论
在 Global.asax 中,我使用以下代码。我获取 JSON 的 URI 是http://www.digantakumar.com/api/values?json=true
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json"));
}
评论
不要使用浏览器来测试 API。
相反,请尝试使用允许您指定请求的 HTTP 客户端,例如 CURL 甚至 Fiddler。
此问题出在客户端中,而不是 API 中。Web API 根据浏览器的请求正常运行。
评论
如果在 中执行此操作,默认情况下将获得 JSON,但如果您作为请求标头传递,它仍然允许您返回 XML。WebApiConfig
text/xml
Accept
注意:这将删除对application/xml
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
如果您没有使用 MVC 项目类型,因此一开始就没有此类,请参阅此答案,了解有关如何合并它的详细信息。
评论
application/xml
*/*
application/xml
注意:阅读此答案的评论,如果您使用 WebAPI 的默认错误处理,它可能会产生 XSS 漏洞
我只是在我的 MVC Web API 项目的类中添加以下内容。App_Start / WebApiConfig.cs
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("text/html") );
这样可以确保在大多数查询上获得 JSON,但可以在发送 .XML
text/xml
如果您需要回复,请查看下面的 Todd 答案。Content-Type
application/json
NameSpace
正在使用 。System.Net.Http.Headers
评论
Content-Type
text/html
在 WebApiConfig.cs 中,添加到 Register 函数的末尾:
// Remove the XML formatter
config.Formatters.Remove(config.Formatters.XmlFormatter);
来源。
评论
当标题包含“Chrome”时,我使用全局操作过滤器来删除:Accept: application/xml
User-Agent
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();
}
}
似乎有效。
这段代码使 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"));
谢谢大家!
评论
由于该问题是特定于 Chrome 的,因此您可以获取 Postman 扩展程序,该扩展程序允许您设置请求内容类型。
评论
network.http.accept.default
text/html,application/xhtml+xml,application/json;q=0.9,application/xml;q=0.8,*/*;q=0.7
text/html,application/xhtml+xml;q=1.0,*/*;q=0.7
我最喜欢 Felipe Leusin 的方法 - 确保浏览器获得 JSON,而不会影响实际需要 XML 的客户端的内容协商。对我来说,唯一缺少的部分是响应标头仍然包含 content-type:text/html。为什么会有问题?因为我使用的是 JSON 格式化程序 Chrome 扩展程序,它会检查内容类型,而且我没有得到我习惯的漂亮格式。我用一个简单的自定义格式化程序修复了这个问题,该格式化程序接受文本/html 请求并返回 application/json 响应:
public class BrowserJsonFormatter : JsonMediaTypeFormatter
{
public BrowserJsonFormatter() {
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
this.SerializerSettings.Formatting = Formatting.Indented;
}
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) {
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue("application/json");
}
}
像这样注册:
config.Formatters.Add(new BrowserJsonFormatter());
评论
this.SerializerSettings.Formatting = Formatting.Indented;
using System.Net.Http.Formatting
using Newtonsoft.Json
使用 RequestHeaderMapping 效果更好,因为它还可以设置响应标头,这允许 Firefox(带有 JSONView 插件)将响应格式化为 JSON。Content-Type = application/json
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings
.Add(new System.Net.Http.Formatting.RequestHeaderMapping("Accept",
"text/html",
StringComparison.InvariantCultureIgnoreCase,
true,
"application/json"));
评论
来自 MSDN 使用 ASP.NET 和 AngularJS 构建单页应用程序(约 41 分钟)。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// ... possible routing etc.
// Setup to return json and camelcase it!
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}
它应该是最新的,我试过了,它奏效了。
我不清楚为什么答案会有所有这些复杂性。当然,有很多方法可以做到这一点,包括 QueryStrings、标头和选项......但我认为最好的做法很简单。你请求一个纯 URL(例如:),作为回报,你会得到 JSON。您将获得具有正确响应标头的 JSON:http://yourstartup.com/api/cars
Content-Type: application/json
在寻找同一个问题的答案时,我找到了这个线程,并且不得不继续前进,因为这个公认的答案并不完全有效。我确实找到了一个答案,我觉得这个答案太简单了,不能成为最好的答案:
我也会在这里添加我的提示。
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 默认值,还是可能在其他地方创建(由我的项目中的其他人创建)。安利,希望这会有所帮助。
自提出(并回答)此问题以来已经过去了一段时间,但另一种选择是在请求处理期间使用 MessageHandler 覆盖服务器上的 Accept 标头,如下所示:
public class ForceableContentTypeDelegationHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
var someOtherCondition = false;
var accHeader = request.Headers.GetValues("Accept").FirstOrDefault();
if (someOtherCondition && accHeader.Contains("application/xml"))
{
request.Headers.Remove("Accept");
request.Headers.Add("Accept", "application/json");
}
return await base.SendAsync(request, cancellationToken);
}
}
哪里可以是任何东西,包括浏览器类型等。这将适用于条件情况,在这种情况下,我们只希望有时覆盖默认内容协商。否则,根据其他答案,您只需从配置中删除不必要的格式化程序即可。someOtherCondition
当然,您需要注册它。您可以全局执行此操作:
public static void Register(HttpConfiguration config) {
config.MessageHandlers.Add(new ForceableContentTypeDelegationHandler());
}
或逐条路线:
config.Routes.MapHttpRoute(
name: "SpecialContentRoute",
routeTemplate: "api/someUrlThatNeedsSpecialTreatment/{id}",
defaults: new { controller = "SpecialTreatment" id = RouteParameter.Optional },
constraints: null,
handler: new ForceableContentTypeDelegationHandler()
);
由于这是一个消息处理程序,它将在管道的请求端和响应端运行,就像 .因此,您可以使用自定义标头轻松确认覆盖:HttpModule
public class ForceableContentTypeDelegationHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
var wasForced = false;
var someOtherCondition = false;
var accHeader = request.Headers.GetValues("Accept").FirstOrDefault();
if (someOtherCondition && accHeader.Contains("application/xml"))
{
request.Headers.Remove("Accept");
request.Headers.Add("Accept", "application/json");
wasForced = true;
}
var response = await base.SendAsync(request, cancellationToken);
if (wasForced){
response.Headers.Add("X-ForcedContent", "We overrode your content prefs, sorry");
}
return response;
}
}
这是一个类似于 jayson.centeno 和其他答案的解决方案,但使用来自 的内置扩展。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 的$format,但它也适用于非 OData 实现,并在响应中返回标头。Content-Type: application/json; charset=utf-8
它允许您在使用浏览器进行测试时标记或到 uri 的末尾。在使用非浏览器客户端时,它不会干扰其他预期行为,您可以在其中设置自己的标头。&$format=json
&$format=xml
你只需像这样改变:App_Start/WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
//Below formatter is used for returning the Json result.
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
//Default route
config.Routes.MapHttpRoute(
name: "ApiControllerOnly",
routeTemplate: "api/{controller}"
);
}
评论
这是我在应用程序中使用的最简单的方法。在函数中添加以下给定的 3 行代码:App_Start\WebApiConfig.cs
Register
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
Asp.net Web API 会自动将返回对象序列化为 JSON,并在标头中添加 时,以便浏览器或接收方了解您正在返回 JSON 结果。application/json
WebApiConfig 是可以配置是要以 json 格式输出还是以 xml 格式输出的位置。默认情况下,它是 xml。在 register 函数中,我们可以使用 HttpConfiguration 格式化程序来格式化输出。
System.Net.Http.Headers => MediaTypeHeaderValue("text/html")
需要获取 JSON 格式的输出。
在最新版本的 ASP.net WebApi 2 中,在 下,这将起作用:WebApiConfig.cs
config.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
config.Formatters.Add(GlobalConfiguration.Configuration.Formatters.JsonFormatter);
只需在 WebApiConfig 类上添加这两行代码即可
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//add this two line
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
............................
}
}
返回正确的格式由媒体类型格式化程序完成。
正如其他人所提到的,你可以在课堂上这样做:WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
...
// Configure Web API to return JSON
config.Formatters.JsonFormatter
.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
...
}
}
有关更多信息,请查看:
如果您的操作返回 XML(默认情况下是这种情况),并且您只需要一个特定的方法来返回 JSON,则可以使用 并将其应用于该特定操作。ActionFilterAttribute
筛选器属性:
public class JsonOutputAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
ObjectContent content = actionExecutedContext.Response.Content as ObjectContent;
var value = content.Value;
Type targetType = actionExecutedContext.Response.Content.GetType().GetGenericArguments()[0];
var httpResponseMsg = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
RequestMessage = actionExecutedContext.Request,
Content = new ObjectContent(targetType, value, new JsonMediaTypeFormatter(), (string)null)
};
actionExecutedContext.Response = httpResponseMsg;
base.OnActionExecuted(actionExecutedContext);
}
}
应用于操作:
[JsonOutput]
public IEnumerable<Person> GetPersons()
{
return _repository.AllPersons(); // the returned output will be in JSON
}
请注意,您可以省略动作装饰上的单词,并使用 just 代替 .Attribute
[JsonOutput]
[JsonOutputAttribute]
评论
上面的大多数答案都非常有道理。 由于您看到的数据以 XML 格式格式化,这意味着应用了 XML 格式化程序,因此您只需从 HttpConfiguration 参数中删除 XMLFormatter 即可查看 JSON 格式,例如
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.EnableSystemDiagnosticsTracing();
}
因为JSON是默认格式
config.Formatters.Remove(config.Formatters.XmlFormatter);
评论
我很惊讶地看到这么多回复需要编码来更改一个 API 中的单个用例 (GET),而不是使用适当的工具,这些工具必须安装一次,并且可用于任何 API(自己的或第三方)和所有用例。
所以好的答案是:
- 如果只想请求 json 或其他内容类型,请安装 Requestly 或类似工具,并修改 Accept 标头。
- 如果您也想使用 POST 并且格式良好的 json、xml 等,请使用适当的 API 测试扩展,例如 Postman 或 ARC。
评论
您可以按如下方式使用:
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
评论
多年来,在最近更新了核心库和 Json.Net 库后,我使用了 Felipe Leusin 的答案,遇到了 :SupportedMediaTypes。
就我而言,解决方案是安装 .在某些情况下,NuGet 显然会删除它。手动安装后,问题已解决。System.MissingMethodException
System.Net.Http
评论