33

A simple task in MVC, sometimes becomes a hard challenge.

Well,i have an Area called Admin. I've a page named "Forbidden" inside the Shared's directory in this area.

The goal is simple: I need to create an Html.ActionLink that generates a link to return to Home page which is OUTSIDE the Admin area.

So i try,<%= Html.ActionLink("Back","Index",new {controller="Home"})%>,and its generate :

http://localhost/Admin/Home/Index

Its wrong!I want:

http://localhost/Home/Index

How can i create a link from an area to the default controllers structure?

Cornelius
  • 830
  • 11
  • 31
ozsenegal
  • 4,055
  • 12
  • 49
  • 64

1 Answers1

57

Try this :

<%= Html.ActionLink("Back", "Index", "Home", new { area = "" }, null) %> 

When using Areas, you should always specify the area your are calling in your ActionLinks by adding a route value as above, If the link is outside the area (as in your case), just use an empty parameter for the area.


There's a nice extension that i find essential in any ASP.NET MVC project (T4MVC). It makes your ActionLinks look much cleaner and it protects them against errors.

So the above code will look something like this :

<%= Html.ActionLink("Back", MVC.Home.Index()) %>

and when using an area :

<%= Html.ActionLink("Some Link", MVC.Admin.SomeController.SomeAction()) %>

It's a part of the MvcContrib project on codeplex here

You should consider using it.

Manaf Abu.Rous
  • 2,397
  • 21
  • 24
  • I tried to get tricky and add this to my AreaRegistration default route, but that didn't work. The manual direct to area = "" works in a regular action link. Thanks. – kmehta Mar 30 '12 at 23:17
  • 1
    And for Form this is how you would do it. `Html.BeginForm("LogOff", "Account", new {area = ""}, FormMethod.Post, new { id = "logoutForm" }))` – Rosdi Kasim May 24 '13 at 06:10