0

I'm trying to build a REST API with ASP mvc and i'm having some trouble with the routing.

I would like to match the following urls in a nice and convenient way:

https://foo.com/collections/
https://foo.com/collections.json/
https://foo.com/collections.xml/

https://foo.com/collections/collectionID/
https://foo.com/collections/collectionID.json/
https://foo.com/collections/collectionID.xml/

And in future more items on the same pattern:

https://foo.com/persons/
https://foo.com/persons.json/
https://foo.com/persons.xml/

https://foo.com/persons/personID/
https://foo.com/persons/personID.json/
https://foo.com/persons/personID.xml/

My best attempt so far is:

routes.MapRoute(
    name: "RESTCollections",
    url: "{controller}s/",
    defaults: new { controller = "Collection", action = "Index" }
);

routes.MapRoute(
    name: "RESTCollections",
    url: "{controller}s/{format}/",
    defaults: new { controller="Collection", action="Index", format = UrlParameter.Optional },
    constraints: new { format = "json|xml|html" }
);

It manages to match:

https://foo.com/collections/
https://foo.com/collections/json

But I'm stuck there trying to replace the "/" between the controller and format gives 404. Simply removing the "/" also gives 404.

SverkerSbrg
  • 503
  • 1
  • 9
  • 23
  • for using period in mvc urls see http://stackoverflow.com/questions/9273987/asp-net-mvc-url-route-supporting-dot – Taher Rahgooy Jul 16 '15 at 18:37
  • possible duplicate of [ASP.NET MVC: How to Route Search Term with . (Period) at the end](http://stackoverflow.com/questions/332757/asp-net-mvc-how-to-route-search-term-with-period-at-the-end) – Daniel Hoffmann-Mitscherling Jul 16 '15 at 18:37
  • Are you using Web Api? If so, you can route your methods like this: [HttpGet] [Route("api/persons/] public IHttpActionResult GetAll() { ... } [HttpGet] [Route("api/persons.json/] public IHttpActionResult GetAllJson() { ... } [HttpGet] [Route("api/persons.xml/] public IHttpActionResult GetAllXml() { ... } – Ricardo Pontual Jul 16 '15 at 18:47

1 Answers1

0

To answer your question you could try

 `context.MapRoute(
    "Api_default",
    "{controller}/{action}.{format}",
    new { controller = "Home", action = "Index", format = UrlParameter.Optional });`

As stated here stackoverflow.com/.../customizing-asp-net-mvc-routing-to-service-json-xml-style-urls

On the other hand I don't see why you would like to state which format you want to accept to be sent to your API.

You could use the [FromBody] string content which deserialises the sent value to the method.

Or you could define the type of the parameter with the Route attribute

[Route("Post/{collection:string}")
public void Post(string collection)

Where string is a predefined constraint, you can create your own custom constraints.

Community
  • 1
  • 1
David
  • 703
  • 11
  • 24
  • I just want the .json declaration as an overrride of the normal accept-header content negotiation. – SverkerSbrg Jul 17 '15 at 06:59
  • I think my main problem is that ISS is not built to handle dots in urls, it is possible but seems to require an unsafe setting. – SverkerSbrg Jul 17 '15 at 09:11