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 productslocalhost: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 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
Thanks in advance for the responses.
Edit 1: Changed exten
to ext
after a comment. Still the same errors..