WebApI 2输出出错 [英] Error in WebApI 2 output

查看:43
本文介绍了WebApI 2输出出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在网络Api结果输出如下,而不是json



此XML文件似乎没有与之相关的风格信息。文档树如下所示。 
< ArrayOfstring < span class =code-attribute> xmlns:i = http://www.w3。 org / 2001 / XMLSchema-instance xmlns = http://schemas.microsoft.com/2003/10/Serialization/Arrays > ;
< string > X < / string > ;
< string > Y < / string >
< / ArrayOfstring >





我的控制器:



  public   class  ValuesController:ApiController 
{
public IHttpActionResult Get()
{
return Ok( new [] { X Y});
}
}





WebConfig.cs:



  public   static   void 注册(HttpConfiguration配置)
{
// Web API配置和服务

// Web API路由
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: DefaultApi
routeTemplate: api / {controller} / {id}
默认值: new {id = RouteParameter.Optional}
);
}





我希望json输出{X,Y}但它会产生上面的结果输出

解决方案

Web API默认返回XML和JSON。您可以覆盖行为以始终返回首选结果。通过添加标头或更改Web API设置。



一种简单的方法是删除配置本身的XML支持,



< pre lang =c#> config.Formatters.JsonFormatter.SupportedMediaTypes.Add(
new MediaTypeHeaderValue( text / html
);





我谈到的另一种方法是添加Content-Type标头并将值设置为application / json。或者当您添加一个标题(此标题与Request,通常使用HttpClient本身)Accept并将其设置为application / json时。



就个人而言,Web API总是在我的情况下返回JSON格式的内容。我从来没有偶然发现我获得XML数据的情况。



以下是来自互联网的有用主题:



http://stackoverflow.com/questions/ 9847564 / how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome [ ^ ]

https://github.com/aspnet/Mvc/issues/1765 [ ^ ]

http:// www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api [ ^ ]



如果您使用jQuery来获取数据,则应始终考虑使用


.getJSON()函数。它为您自己完成所有标题设置。 : - )

Hi I got an output in web Api result as below instead of json

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>X</string>
<string>Y</string>
</ArrayOfstring>



My Controller:

public class ValuesController : ApiController
{
       public IHttpActionResult Get()
       {
           return Ok(new[]{"X","Y"});
       }
 }



WebConfig.cs :

public static void Register(HttpConfiguration config)
{
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
}



I expect json output {"X","Y"} but it produces above result output

解决方案

Web API, by default returns XML and JSON. You can override the behavior to always return a preferred result. Either by adding a header, or by changing the Web API setting.

One easy way to do this is to remove the XML support in the configuration itself,

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(
   new MediaTypeHeaderValue("text/html") 
);



Another way that I talked about was to add "Content-Type" header and set the value to "application/json". Or when you add a header (this header goes with the Request, typically with the HttpClient itself) of "Accept" and set it to "application/json".

Personally saying, Web API always returns JSON format content in my case. I have never stumbled upon a case where I get to have the XML data.

Here are the helpful threads from internet:

http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome[^]
https://github.com/aspnet/Mvc/issues/1765[^]
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api[^]

If you are using jQuery to get the data, you should always consider using


.getJSON() function. It does all of the header settings for you itself. :-)


这篇关于WebApI 2输出出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆