13
votes

I'm working on a new project where I want to use Phil Haack Areas (1) idea + Steve Sanderson's tweak (2). I have a simple root view with an action link to a view an area (Foo). The URL that is generated has the proper area, but it appends the root controller (Bar) at the end. Here's my root view code:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

<%= Html.ActionLink("Foo Index Page", "Index", new { area="Foo" } )%>

</asp:Content>

and here's the URL that it generates:

localhost:6494/Foo/Bar

Any idea why the "/Bar" is on there?

(1): haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

(2): blog.codeville.net/2008/11/05/app-areas-in-aspnet-mvc-take-2/

3

3 Answers

20
votes

I've found a solution. I don't think it is appropriate, so I will ask for an improvement. By specifying a controller name, I can get the URL to form properly. I.E.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

<%= Html.ActionLink("Foo Index Page", "Index", new { area="Foo", controller="Baz" } )%>

</asp:Content>

Once I did this, then the URL was correct

localhost:6494/Foo

Why is this a problem? Phil's demo uses the controller with the name HomeController. I don't know (because I can't trace) how the Html.ActionLink() method goes about constructing the URL; but it looks as if it is relying on a default case of HomeController existing, which I don't have.

If anyone has a suggestion on how to allow for Controllers not named Home to be the default, please reply. Thanks

2
votes

To not have Home as the default controller name, simply change the default route.

0
votes

@Joe

In your App_Start/RouteConfig.cs file you have to change value for default controller from Home to whatever you like :

routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "NAME_OF_YOUR_HOME_CONTROLLER", action = "Index", id = "" }  // Parameter defaults
        );