REST Style URL Routing

I want to configure routing to allow the following:

/entity/id          //id is Guid
/entity/id          //id is int32
/entity/actionname  //actionname is string matching neither Guid nor Int

Simple, I thought what RouteConstraints solves:

routes.MapRoute(
    name: "entity/id:guid",
    template: "{controller}/{id:guid}",
    defaults:new{action="Index"});
routes.MapRoute(
    name: "entity/id:int",
    template: "{controller}/{id:int}",
    defaults: new { action = "Index" });
routes.MapRoute(
    name: "entity/action",
    template: "{controller=home}/{action=index}");

where, in my opinion, the order matters: more specific matches should go to the entity / action route, which otherwise would correspond to all.

But that does not work.

@Url.Action("", "entity", new { id = Guid.NewGuid() })

leads to

entity?id=00000000-0000-0000-0000-000000000000

instead

entity/00000000-0000-0000-0000-000000000000

How can i fix this?

(I am on asp.net Core 1.1, although I believe the question is true for MVC4)

+4
source share
1 answer

It will generate the expected result if you pass the action name "Index"or pass nullas the first parameter.

+2
source

Source: https://habr.com/ru/post/1673147/


All Articles