johndoe
johndoe

Reputation: 25

ASP.NET MVC Routing Problem!

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

       );

Upvotes: 2

Views: 114

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

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.

Upvotes: 2

Peter Mourfield
Peter Mourfield

Reputation: 1885

What about this?

      routes.MapRoute(
              "ArticleDetail",
              "{controller}/{id}",
              new { controller = "Articles", action = "Details" },
              new { id = @"\d+" }
        );

     routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "List", id = "" }
        );

Upvotes: 0

Related Questions