Keith Sirmons
Keith Sirmons

Reputation: 8411

Why is @Html.ActionLink leaving off the controller and action portions of the link?

I am creating a link inside the main _Layout.cshtml file inside a MVC3 application.

@Html.ActionLink("Security", "Index", "Home", new { area = "Security" }, new { })

When the page is rendered, this is the resultant Html

<a href="/Security">Security</a>

and if I click on this link, I get a blank page.

I have routedebugger installed and here are the results:

Route Debugger output

I have the default route inside the Global.ascx.cs as follows:

 routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "Eis.Mvc.Web" }// Parameter defaults
        );

and the Security Area's route:

context.MapRoute(
            "Security_default",
            "Security/{controller}/{action}/{id}",
            new { area = "Security", controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "Eis.Mvc.Web.Areas.Security.Controllers" }
        );       

If I directly type in the following URL, http://localhost:1410/Security/home, the correct page is presented.

How can I get the @Html.Action link to include the controller portion of the URL?

I do have a Home controller with an Index action inside the root portion of the application and had to include the Namespace filters to the route registrations.

Thank you, Keith

Upvotes: 0

Views: 324

Answers (1)

Keith Sirmons
Keith Sirmons

Reputation: 8411

Change the Security Area's route mapping to the following:

context.MapRoute(
        "Security_default",
        "Security/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new string[] { "Eis.Mvc.Web.Areas.Security.Controllers" }
    );    

Notice the area and controller portions of the defaults parameter were removed.

Now the @Html.ActionLink("Security", "Index", "Home", new { area = "Security" }, new { }) renders <a href="/Security/Home">Security</a>

Keith

Upvotes: 0

Related Questions