I am trying to do a very simple thing. I want that when I type
Articles/list
then it should invoke the index action and list all the articles.
When I type
Articles/3
It should invoke the Index action and show the article detail. How can I achieve this? Here is my Global.asax routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Articles", action = "List" } // Parameter defaults
);
routes.MapRoute(
"ArticleDetail", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Articles", action="Index", id = "" } // Parameter defaults
);
I think you can do this without route constraints.. try:
routes.MapRoute( "ListArticles", // Route name "Articles/List", // URL with parameters new { controller = "Articles", action = "List" } ); routes.MapRoute( "ArticleDetails", // Route name "Articles/{id}", // URL with parameters new { controller = "Articles", action = "Index" } );if not add
new {id = @”\d+” } after the Index item above – but it should work ok.