zoukankan      html  css  js  c++  java
  • ASP.NET Web API在Firefox下总显示为XML格式,如何切换为JSON格式?

    看了一下ASP.net Web API,发现使用Web API创建RESTful服务比WCF还是方便很多了。因为我使用的是Firefox下的一个插件RESTClient做测试,在使用Firefox测试时发现,返回的结果总是XML格式的,而不是我想要的JSON格式,如果使用IE的话倒是JSON格式。原因在于Firefox默认的header信息Accept是Application/xml,而IE是Application/json. 于是对应的解决办法就是添加Header

    Accept:application/json

    201302161504.jpg

    还有一个办法就是在服务器段更改,让web API忽略客户端的设置,在global.asax.cs里面加上GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();


      protected void Application_Start()

    {

    AreaRegistration.RegisterAllAreas();


    WebApiConfig.Register(GlobalConfiguration.Configuration);

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    RouteConfig.RegisterRoutes(RouteTable.Routes);

    BundleConfig.RegisterBundles(BundleTable.Bundles);


    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();


    }


  • 相关阅读:
    第二天续
    使用git提交本地仓库每次需要输入账号密码的问题解决
    第二天
    开启远程之路
    第一天
    第一步了解并且安装配置
    6
    Algorithms
    Algorithms
    Algorithms
  • 原文地址:https://www.cnblogs.com/junqilian/p/2913414.html
Copyright © 2011-2022 走看看