2

I would like to have 2 routes similar to these:

routes.MapRoute("Detail", "guide/{urlname}", new { controller = "Application", action = "Detail" });
routes.MapRoute("Search", "guide/{keyword1}/{keyword2}", new { controller = "Guide", action = "Index", keyword1 = UrlParameter.Optional, keyword2 = UrlParameter.Optional });

So one route is a detail page that looks up an object in the database based on its url name, and the other route is a search results page based on application-generated keywords, both of which share the same url root (/guide). The two actions are in different controllers. Possible urls are:

/guide/evernote        --> should route to the application detail page
/guide                 --> should route to search results without filter
/guide/iphone          --> should route to iphone apps search results
/guide/iphone/medical  --> should route to medical iphone apps search results

Obviously, like this, the second route will never be matched for a url like /guide/iphone because the first route will already match the same url.

I don't want to do a redirect in the first action if the controller can't find the object in the database. So what other alternatives are there? Do I need to create a custom RouteHandler or UrlRoutingModule for this or is there a simpler way?

2 Answers 2

1

If {urlname} is a url like it implies, you can add a constraint to test if the url matches a regex:

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs

Sign up to request clarification or add additional context in comments.

1 Comment

It is not a url, it is the unique name of the object when used as part of a url. It is not possible to make a distinction with a regex.
0

Change the order and it will work:

routes.MapRoute("Search", "guide/{keyword1}/{keyword2}", new { controller = "Guide", action = "Index" });
routes.MapRoute("Detail", "guide/{urlname}", new { controller = "Application", action = "Detail" });

3 Comments

No, because then a detail page will go to the search action with the object's url name as keyword1. The keywords are optional so it will match this route with only one keyword.
thats true, i removed the optionals from my answer. both keywords should be optionals? is not the same if you use query string for parameters? example: guide/{urlname} for url guide?keyword1=k1&keyword2=k2
for SEO purposes the keywords must be in the url. They are not user generated keywords, but application generated keywords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.