0

Tyring to apply formatting on json and xml at once in a web service:

config.Routes.MapHttpRoute(
                name: "DefaultApiWithExtension2",
                routeTemplate: "api/{controller}/{id}.{ext}",
                defaults: null,
                constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
            );

        config.Routes.MapHttpRoute(
            name: "DefaultApiWithExtension1",
            routeTemplate: "api/{controller}.{ext}",
            defaults: null,
            constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

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

        config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "application/xml");
        config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json");

But I'm keep getting 400 error, when I'm trying to get response with extension (and without it). None of the routes works:

api/person.xml

api/person

api/person/1.json

api/person/1

If I'm using only "DefaultApi" route - everithing is ok (with no extensions I'm getting json). What am I'm missing?

FtarG
  • 1
  • 1
  • WebApi can return wide range of formats if setup correctly, and you can define you desired type by sending it in the header. Check [this](https://stackoverflow.com/questions/35722586/header-parameters-accept-and-content-type-in-a-rest-context) question. – AndrasCsanyi Oct 12 '17 at 19:32
  • Agreed, I'm using YARC to test it, and send proper payload to the service, but it doesn't rote to the Get method – FtarG Oct 12 '17 at 19:44
  • Have you checked the documentation? – AndrasCsanyi Oct 12 '17 at 19:59
  • This looks correct. I've gone over your code with a fine-tooth comb and can't spot the error. –  Oct 12 '17 at 21:14

0 Answers0