I am working on an web ASP.NET MVC, I want all the pages on my web to have a unique URL form that looks like this: WebName/{Title} . This is requirement of my customer.
For example: store.com/chicken-pizza and store.com/how-to-cook-beefsteak, but not: store.com/foods/chicken-pizza and store.com/recipe/how-to-cook-beefsteak
I tried to using RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
/* store.com/foods => This works well */
routes.MapRoute(
name: "List-Foods",
url: "foods",
defaults: new { controller = "Food", action = "ListFoods", id = UrlParameter.Optional }
);
/* store.com/chicken-pizza, store.com/cheese-sandwich,... => This works well */
routes.MapRoute(
name: "Detail-Food",
url: "{title}",
defaults: new { controller = "Food", action = "FoodDetail", id = UrlParameter.Optional }
);
/* store.com/recipes => This works well */
routes.MapRoute(
name: "List-Recipes",
url: "recipes",
defaults: new { controller = "Recipe", action = "ListRecipes", id = UrlParameter.Optional }
);
/* store.com/how-to-make-beefsteak,
store.com/instructions-for-making-cookies,..
=> Conflict occurred... this route can't be touch because it
has the same url form as Details-Food (url:{title}) */
routes.MapRoute(
name: "Detail-Recipe",
url: "{title}",
defaults: new { controller = "Recipe", action = "RecipeDetail", id = UrlParameter.Optional }
);
...............
}
I realized that routes.MapRoute(s) cannot have the same URL form (url:"{title}"). That is not a problem for url:"foods" (to get a list of foods) and url:"recipes" (to get a list of recipes) because I have specified the words(foods and recipes) in routes.Maproute. Also, I can get detailed information of any feed by {title} with Detail-Food route easily. But, the problem occurred at Detail-Recipe route, because it has the same url form (url:{title}) as Detail-Food route, I can't touch Recipe/RecipeDetail to get data.