Is it possible to create a route without action?
I have this default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But I also I want to have URLs like this: http://mysite/bar/1234 where 1234 is the ID and bar is the controller.
So I created the following route:
routes.MapRoute(
name: "BarRoute",
url: "{controller}/{id}",
defaults: new { controller = "bar", action = "Index", id = UrlParameter.Optional }
);
But when I navigate to http://mysite/bar/1234, it said the resource is not found. What did I do wrong in the second route?
You cannot have the following 2 routes in that order without any constraints
{controller}/{action}/{id}{controller}/{id}Those 2 routes are incompatible. When you attempt to access
http://mysite/bar/1234, the routing engine is analyzing your routes and/bar/1234matches your first route. Except that I guess you do not have an action called1234on theBarcontroller.So if you want this setup to work you need to specify some
constraints. Also don’t forget that the order of your routes definition is important because they are resolved in the order you defined them. So make sure you place more specific routes at the top.