C# Web API Either XML or JSON based on GET request

别等时光非礼了梦想. 提交于 2019-12-12 12:07:56

问题


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:

  • localhost:port/api/products - get a full list of products
  • localhost:port/api/products/# - get a single product with the given id

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).

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);

so my response would always be in JSON.

Everything works as intented at this point, and I'm getting JSON-format responses on the two GET-requests mentioned above.


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.

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");

I'm getting the 404 errors below:

On localhost:post/api/products:

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:

  • localhost:port\api\products - get a list of products in default browser format
  • localhost:port\api\products\# - get a single product in default browser format
  • localhost:port\api\products.xml - get a list of products in XML format
  • localhost:port\api\products.json - get a list of products in JSON format
  • localhost:port\api\products\#.xml - get a single product in XML format
  • localhost:port\api\products\#.json - get a single product in JSON format

Thanks in advance for the responses.


Edit 1: Changed exten to ext after a comment. Still the same errors..


回答1:


The post you linked to says:

Remember in this example, you still have to issue the request with the appropriate content-type.

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.

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.

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.

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!



来源:https://stackoverflow.com/questions/23366755/c-sharp-web-api-either-xml-or-json-based-on-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!