unforgiven1987
unforgiven1987

Reputation: 464

.Net MVC Route with ID only

I'm trying to configure my routes so that I can have a blog entry (with a string id) be the only segment in the url.

For example,

/ABlogTitle -> Controller = "Blog", Action = "Entry", Id = "ABlogTitle"

My assumption is that if a route fails due to the action not existing, it will retry using the next route, but that doesn't seem to be working.

Here are my routes...

routes.MapRoute(
    name: "Entries",
    url: "{id}",
    defaults: new {controller = "Blog", action = "Entry"}
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
);

Upvotes: 0

Views: 579

Answers (2)

Jeremy Bell
Jeremy Bell

Reputation: 718

You would need to use a Regular Expression-like RouteContraint to limit the matching or use a prefix like /entry/{id}.

Otherwise, yes, the route you have setup for "Entries" will match every URL.

http://www.dotnet-tricks.com/Tutorial/mvc/KIWF010113-Route-Constraints-in-Asp.Net-MVC-with-example.html

Or check out the new routing available in the latest MVC and Web API releases:

http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

Upvotes: 0

Kiran
Kiran

Reputation: 57949

No, your assumption is incorrect. When a request matches a route, MVC doesn't go through rest of the routes in the collection.

In this case the request /ABlogTitle matches the 1st route in the collection and no more route probing is done and rest of the pipeline (controller selection, action selection etc.) takes place.

Upvotes: 1

Related Questions