使用更新的ASP。NET Web API,在Chrome中我看到XML -我如何将其更改为请求JSON,以便我可以在浏览器中查看它?我相信这只是请求头的一部分,我是正确的吗?


当前回答

在最近更新了核心库和Json之后,多年来一直使用Felipe Leusin的答案。Net,我遇到了System.MissingMethodException:SupportedMediaTypes。 在我的案例中,解决方案是安装System.Net.Http,希望对遇到同样意外异常的其他人有所帮助。NuGet显然会在某些情况下删除它。手动安装后,该问题得到了解决。

其他回答

在WebApiConfig.cs中,在Register函数的末尾添加:

// Remove the XML formatter
config.Formatters.Remove(config.Formatters.XmlFormatter);

源。

MVC4快速提示#3 -从ASP中删除XML格式化器。Net Web API

在全球。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();
}

我发现Chrome应用“高级REST客户端”非常适合使用REST服务。你可以将Content-Type设置为application/json。 高级REST客户端

当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();
    }
}

似乎有用。

返回正确的格式是由媒体类型格式化程序完成的。 正如其他人提到的,你可以在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"));

        ...
    }
}

欲了解更多信息,请查看:

ASP中的媒体格式化器。NET Web API ASP中的内容协商。NET Web API。

如果您的操作返回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
}

注意,您可以省略动作修饰上的Attribute这个词,只使用[JsonOutput]而不是[JsonOutputAttribute]。