[RESOLVED]Route Mapping in Areas

Hi all,

How would I create a route mapping to the following url:

http://localhost/SiteName/AdminCP/Topics/EditTopic/28

AdminCP is an area — I can get to the Topics controller and show a list of topics to the user, then the user clicks a link to edit the topic which should

take them to the url above. EditTopic is a controller in AdminCP which returns a single Index method.

This is my AdminCP registration code which is not working.

 public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "AdminCP_default",
                "AdminCP/{controller}/{action}/{id}",
                new { controller= "Home", action = "Index", id = UrlParameter.Optional }
            );

            context.MapRoute(
               "AdminCP_Topics",
               "AdminCP/Topics/{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );

        }

Thanks
Melt

Meltdown

ow would I create a route mapping to the following url:

http://localhost/SiteName/AdminCP/Topics/EditTopic/28

It is already created

Meltdown

context.MapRoute(
                "AdminCP_default",
                "AdminCP/{controller}/{action}/{id}",
                new { controller= "Home", action = "Index", id = UrlParameter.Optional }
            );

What is the problem?

Hi Meltdown,

According to your description, you are have the SiteName in your request URL. If you are using the local IIS and create an application (name is the SiteName) under the default web site, then your routing is correct.

If you are just create a website, then you need add the SiteName in your routing. The code would be like this:

 public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "AdminCP_default",
                "SiteName/AdminCP/{controller}/{action}/{id}",
                new { controller= "Home", action = "Index", id = UrlParameter.Optional }
            );

            context.MapRoute(
               "AdminCP_Topics",
               "SiteName/AdminCP/Topics/{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );

        }

Best Regards

Starain Chen

Hi thanks for the replies, I finally figured this one out.

The simple solution to my problem is to just add an EditTopic action to my Topic controller, but I wanted to maintain the pattern of each
controller only returning a single index action.

The solution is below and it’s important that the EditTopic routing comes before the main generic route.

 public override void RegisterArea(AreaRegistrationContext context)
        {
            
            context.MapRoute(
             "AdminCP_Topic",
             "AdminCP/Topics/EditTopic/{id}",
             new { controller= "EditTopic", action = "Index", id = UrlParameter.Optional }
            );

            context.MapRoute(
                "AdminCP_default",
                "AdminCP/{controller}/{action}/{id}",
                new { controller= "Home", action = "Index", id = UrlParameter.Optional }
            );

            

        }

Leave a Reply