ASP MVC Areas and ActionLink
Asked Answered
B

3

13

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/

Bannasch answered 19/5, 2009 at 14:49 Comment(0)
B
20

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

Bannasch answered 19/5, 2009 at 15:43 Comment(0)
J
2

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

Jerryjerrybuild answered 19/5, 2009 at 15:52 Comment(0)
D
0

@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
        );
Dottiedottle answered 27/6, 2013 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.