Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC Areas and ActionLink

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/

like image 323
Joe Avatar asked May 19 '09 14:05

Joe


2 Answers

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

like image 120
Joe Avatar answered Oct 12 '22 22:10

Joe


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

like image 26
Tomas Aschan Avatar answered Oct 12 '22 22:10

Tomas Aschan