ASP.NET MVC `Html.ActionLink` between "Areas"

Solution 1:

Strange indeed. Steps that worked perfectly fine for me:

  1. Create a new ASP.NET MVC 3 application using the default Visual Studio template
  2. Add an area called Admin using Visual Studio designer by right clicking on the project
  3. Add new Controller in ~/Areas/Admin/Controllers/MeetsController:

    public class MeetsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
    
  4. Add a corresponding view ~/Areas/Admin/Views/Meets/Index.cshtml

  5. In the layout (~/Views/Shared/_Layout.cshtml) add links:

    @Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null)
    @Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null)
    
  6. Run the application.

Rendered HTML for the anchors:

<a href="/Admin/Meets">Admin</a>
<a href="/Meets">Admin</a>

As expected the first link works whereas the second doesn't.

So what's the difference with your setup?

Solution 2:

Another option is to utilize RouteLink() instead of ActionLink(), which bypasses the area registrations altogether:

ActionLink version:

Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }, null)

RouteLink version:

Html.RouteLink("Log Off", "Default", 
    new { action = "LogOff", controller = "Account" })

The second parameter is a "Route Name" which is registered in Global.asax.cs and in various 'AreaRegistration' subclasses. To use 'RouteLink' to link between different areas, you only need to specify the correct route name.

This following example shows how I would generate three links to different areas from a shared partial, which works correctly regardless of which area I am 'in' (if any):

@Html.RouteLink("Blog", "Blog_default", 
    new { action = "Index", controller = "Article" })
<br/>
@Html.RouteLink("Downloads", "Download_default", 
    new { action = "Index", controller = "Download" })
<br/>
@Html.RouteLink("About", "Default", 
    new { action = "Index", controller = "About" })

Happy coding!