基于GET请求C#的Web API XML或JSON [英] C# Web API Either XML or JSON based on GET request
问题描述
我config.Routes设置为:
My config.Routes was set to:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
有了这个,我可以使用:
With this I could use:
-
本地主机:端口/ API /产品
- 得到的产品 -
本地主机:端口/ API /产品/#
- 获得与给定id
的完整列表
localhost:port/api/products
- get a full list of productslocalhost:port/api/products/#
- get a single product with the given id
根据单品在我得到一个不同的格式的浏览器(你在火狐和谷歌Chrome为默认值,和JSON在Internet Explorer XML格式)。
Based on the browser I was getting a different format (you get XML format in FireFox and Google Chrome as default, and JSON in Internet Explorer).
我大多需要JSON,所以起初我又说:
I mostly need JSON, so at first I added:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
所以我的反应将永远是JSON。
so my response would always be in JSON.
一切正常在这一点上intented,而我得到上面提到的两个GET-请求JSON格式的响应。
Everything works as intented at this point, and I'm getting JSON-format responses on the two GET-requests mentioned above.
然后我无意中发现的这个StackOverflow的帖子。
认为这将是一个不错的功能,为自己选择要基于对GET请求返回的格式。
I then stumbled on this stackoverflow post. Thought it would be a nice feature to select for yourself which format you want to return based on the GET-request.
然而,当我更换配置.Routes和JSON-只以上代码提到:
However, when I replace the config.Routes and JSON-only code mentioned above for:
config.Routes.MapHttpRoute(
name: "API UriPathExtentsion",
routeTemplate: "api/{controller}.{ext}/{id}",
defaults: new { id = RouteParameter.Optional, ext = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "API UriPathExtension ID",
routeTemplate: "api/{controller}/{id}.{ext}",
defaults: new { id = RouteParameter.Optional, ext = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json");
config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "text/xml");
我收到下面的404错误:
I'm getting the 404 errors below:
在本地主机:交/ API /产品
:
On localhost:post/api/products
:
和本地主机:端口/ API / products.xml
:
and on localhost:port/api/products.xml
:
任何人都知道可能是什么问题?
Anyone know what might be wrong?
另外,我不知道代码的第二件会做完全一样,我想,所以这里的例子请求的列表和我想它返回:
Also, I'm not sure the second piece of code will do exactly as I would like to, so here is a list of example requests and what I would like it to return:
-
本地主机:port\api\products
- 让产品在默认浏览器的格式列表 -
本地主机:port\api\products\#
- 获得在默认浏览器格式的单品 -
本地主机:port\api\products.xml
- 让产品在XML列表格式为: -
本地主机:port\api\products.json
- 获得JSON格式的产品列表 -
本地主机:port\api\products\#的.xml
- 获得一个单品的XML格式 -
本地主机:port\api\products\#以.json
- 获得一个单品以JSON格式
localhost:port\api\products
- get a list of products in default browser formatlocalhost:port\api\products\#
- get a single product in default browser formatlocalhost:port\api\products.xml
- get a list of products in XML formatlocalhost:port\api\products.json
- get a list of products in JSON formatlocalhost:port\api\products\#.xml
- get a single product in XML formatlocalhost:port\api\products\#.json
- get a single product in JSON format
在提前为响应感谢
编辑1:更改分配延长
到外部
注释之后。还是同样的错误。
Edit 1: Changed exten
to ext
after a comment. Still the same errors..
推荐答案
您挂在帖子中写道:
请记住,在这个例子中,你还是要使用适当的内容类型的请求。
Remember in this example, you still have to issue the request with the appropriate content-type.
这意味着,无论谁调用您的API需要设置内容类型要匹配应用程序/ JSON
或文本/ XML
。如果从浏览器中调用您的内容类型将是默认的内容类型,您使用的浏览器,这可能很容易成为文本/纯
。
This means that whoever calls your API need to set the content-type to match either application/json
or text/xml
. If you are calling from a browser your content-type will be the default content-type of the browser you are using, which might easily be text/plain
.
选项1:谷歌和找出如何更改内容类型,你正在使用的浏览器。我从来没有试过这种改变自己
Option 1: Google and find out how to change the content-type of the browser you are using. I have never tried to change this myself.
选项2:如果您从代码中调用这一点,并使用C#,使用的HttpClient
键,呼叫之前,修改的内容类型为的这个计算器帖子
Option 2: If you call this from code and use C#, use the HttpClient
and modify the content-type before your call as explained in this StackOverflow post.
选项3 :使用作曲家在提琴手(免费)打电话给你的服务。有很容易设置内容类型。测试Web API时,我更喜欢这一点。
Option 3: Use the composer in Fiddler (free) to call your service. There it is easy to set the content-type. I prefer this when testing web APIs.
当这一切都说过和做过,我不知道这实际上解决您的问题,但我希望如此。至少你然后按照你的一切链接到说的帖子。祝你好运!
When all this is said and done, I'm not sure this actually solves your problem, but I hope so. At least you then follow everything the post you linked to says. Good luck!
这篇关于基于GET请求C#的Web API XML或JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!