Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC ActionLink outside area

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?

like image 386
ozsenegal Avatar asked Sep 21 '10 00:09

ozsenegal


People also ask

How do I set an area in URL action?

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.

What is the use of ActionLink in MVC?

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.

How do I pass an object in ActionLink?

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).

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

How do I link to an area in MVC?

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).

What is htmlactionlink in MVC with example?

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.

What is an area in ASP NET MVC?

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.

How do I link within an area in Solution Explorer?

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.


1 Answers

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.

like image 71
Manaf Abu.Rous Avatar answered Sep 22 '22 09:09

Manaf Abu.Rous