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?
You can use this Url. Action("actionName", "controllerName", new { Area = "areaName" }); Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.
ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.
If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).
Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
In an ASP.NET MVC area application, you can link within an area as you would in any MVC application. For example, you can call the ActionLink method, or you can call any other routine that takes a controller or action name (such as the RedirectToAction method).
The Html.ActionLink creates an anchor element based on parameters supplied. In this example, we will learn how to use Asp .Net MVC Html.ActionLink. We will also create multiple types of Action links like: HtmlActionLink without controller name. HtmlActionLink with controller name. HtmlActionLink with parameters. HtmlActionLink with Area.
For these types of applications, the default ASP.NET MVC project structure can become unwieldy. To accommodate large projects, ASP.NET MVC lets you partition Web applications into smaller units that are referred to as areas.
In Solution Explorer, open the Global.asax file for the project. Insert the following code into the Application_Start method: This code calls the route registration methods for each child area. In an ASP.NET MVC area application, you can link within an area as you would in any MVC application.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With