1

I have a mvc application Areas folder structure of the following type:

Areas => FolderA => Controllers => ControllerA & ControllerB

I have used the following register path in AreaRegistration:

context.MapRoute(
            "default1",
            "FolderA/{controller}/{action}/{id}",
            new { controller = "ControllerA|ControllerB", action = "Index", id = UrlParameter.Optional }
        ); 

and I have two links on a shared layout as:

@Html.ActionLink("Link 1", "ActionA", "ControllerA", null)
@Html.ActionLink("Link 2", "ActionB", "ControllerB", null)

Link 1 seems to be working fine and redirects as expected. Issue is with Link2, which always forms the following url and i get 404 error.

http://localhost:29661/FolderA/ControllerA/ActionB?Length=15

The default application route path is:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        ); 

Seems like it is always looking for ActionB in the same controller, even i have 2 different paths registered. Can anyone please help it out.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Tech Jay
  • 404
  • 1
  • 6
  • 16

1 Answers1

1

ControllerA|ControllerB is not a valid default value. I believe what you want is a constraint, not a default for your controller. So, your route should look like this instead:

context.MapRoute(
    "default1",
    "FolderA/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { controller = "ControllerA|ControllerB" }
);

But in this case you don't need a constraint because the route to use can be determined by the area name, so you could get by with:

context.MapRoute(
    "default1",
    "FolderA/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

The default you use for controller will be the location it navigates to if there is no controller provided in the URL. You would either use a controller in your area as the default or exclude the default controller to require there to be a controller in the URL.

In any case, you need to provide the area name when building the link to an area. The overload that you are using will not work when you provide a string as the 3rd parameter.

@Html.ActionLink("Link 1", "ActionA", "ControllerA", new { area = "FolderA" }, null)
@Html.ActionLink("Link 2", "ActionB", "ControllerB", new { area = "FolderA" }, null)

And as pointed out here, you need to specify the area even when you want to navigate to a non-area link.

@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I have tried specfying the Area name as well as the routing you mentioned: @Html.ActionLink("Link A", "Method_Name_1", "Controller1", new { area = "Area_1" }) @Html.ActionLink("Link B", "Method_Name_2", "Controller2", new { area = "Area_1" }) But html still renders the second link with the controll2 as the controller name in the action link. Can you suggest what should be the route mapping registration for 2 controllers in the same area ??? – Tech Jay May 02 '15 at 15:56
  • The above example "default1" is what you would normally use for an entire area. The controller name is a variable that is specified by the first segment after the area name in the URL. But do note that the order in which routes are specified is important, and you will get different results if you have a matching route that exists in your configuration before the intended route. `AreaRegistration.RegisterAllAreas()` should be specified first before `RouteConfig.RegisterRoutes()` as well or your default route will override any area routes that you have configured. – NightOwl888 May 02 '15 at 16:09
  • I changed the area url registration to : context.MapRoute( "Area_1", "Area_1/{controller}/{action}/{id}", new { controller = "Controller1|Controller2", action = "Index", id = UrlParameter.Optional } ); But still of no use.I don't understand how the same thing is working out of Areas, with two controllers Home and Login. Both controllers have their own separate view folders and default routing of AppRoute seems to be working fine with it. – Tech Jay May 02 '15 at 16:46
  • I suggest you go through the [MSDN Areas Tutorial](https://msdn.microsoft.com/en-us/library/ee671793%28v=vs.100%29.aspx) or [this tutorial](http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm). Clearly there is something you are overlooking in the configuration that you have not posted in your question, but I can only guess as to what it is. – NightOwl888 May 02 '15 at 17:53
  • @TechJay - I agreed with answer provided by NightOw888. See my views here: http://forums.asp.net/p/2048326/5903140.aspx If it resolves your issue, I suggest mark this as correct answer. If not come back here and lets discuss the issue. – Gaurav Arora May 03 '15 at 13:20
  • @Gaurav sir, you are correct. NightOw888 was right. Actually i missed his point that i was missing the htmlAttribute to be specified as NULL, for the Html.ActionLink overload i was trying to use. – Tech Jay May 03 '15 at 13:56