2

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

       );
2
  • 1
    Shouldn't list and detail be different actions on the Articles controller? Is there a particular reason you are trying to use the Index action for both? Commented Jun 16, 2011 at 20:52
  • List and Index are two separate actions!! Commented Jun 16, 2011 at 20:54

2 Answers 2

2

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.

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

1 Comment

note - I like to spell out my routes specifically and not use {controller}/{action} so I know exactly what is going into the app. just my preference though.
0

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 = "" }
        );

Comments

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.