ASP.NET MVC `Html.ActionLink` between 'scopes'

I added a new area to my MVC3 project, and I'm trying to link it to the _Layout page in a new one. I added an area called "Admin" that has a "Meets" controller.

I used the designer of the visual studio to add an area to have the correct area registration class, etc., and the global.asax file registers all areas.

However, when I use the following 2 action links on a page in the root, I encounter several problems:

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

When I click both links, I get to the Meets controller in the Admin area, where the application then sends an error stating that it cannot find the Index page (even if the Index page is in the Views folder in the Subcategory area.

The link for the first link is as follows:

http://localhost/BCC/Meets?area=Admin

And the href for the second link looks like this:

http://localhost/BCC/Meets

Also, if I click on the link I expect to create:

http://localhost/BCC/Admin/Meets

I just get the resource can not find the error. Everything is very perplexing! Hope someone can help ...

+43
asp.net-mvc razor asp.net-mvc-areas
Mar 25 2018-11-21T00:
source share
6 answers

I realized this - I created a new test project and did the same as before, and it worked ... then, after further inspection of all the routes connected between the two projects, I found a discrepancy.

In the global.asax file in my BCC application, a line of rogue code appeared that inexplicably appeared:

  public static void RegisterRoutes(RouteCollection routes) { // Problem here routes.Clear(); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 

As you can see where my comment is, at some point I placed the routes. Clear () at the beginning of RegisterRoutes, which meant after I registered the Fields in Application_Start, I immediately cleared that I had just registered.

Thanks for the help ... this ultimately led to my salvation!

+7
Mar 25 '11 at 12:30
source share

It’s strange. The steps that worked perfectly for me:

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

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

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

     @Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null) @Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null) 
  • Launch the app.

Rendered HTML for anchors:

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

As expected, the first link works, and the second does not.

So what's the difference with your setup?

+67
Mar 25 '11 at 12:13
source share

Another option is to use RouteLink () instead of ActionLink (), which generally bypass area registration:

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 the “Route Name”, which is registered in Global.asax.cs and in different subclasses of AreaRegistration. To use "RouteLink" to communicate between different areas, you only need to specify the correct route name.

The following example shows how I will generate three links to different areas from a shared partial file that 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!

+28
Jan 21 2018-12-12T00:
source share

Make sure the AdminAreaRegistration class looks like this:

 public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } 

and what do you have in Global.asax.cs :

 protected void Application_Start() { ... // ViewEngine Registration AreaRegistration.RegisterAllAreas(); ... // Other route registration } 
+3
Mar 25 '11 at 12:13
source share

I solved this problem by doing the following.

In my Global.asax.cs I have

  public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); } protected void Application_Start() { //Initialise IoC IoC.Initialise(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 

In my PublicAreaRegistration.cs (Public Area) I have

 public class PublicAreaRegistration : AreaRegistration { public override string AreaName { get { return "Public"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute("Root", "", new { controller = "Home", action = "Index" }); context.MapRoute( "Public_default", "Public/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } , new[] { "<Project Namespace here>.Areas.Public.Controllers" } ); } } 

In my AuthAreaRegistration.cs (Area for Restricted access) I have

 public class AuthAreaRegistration : AreaRegistration { public override string AreaName { get { return "Auth"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Auth_default", "Auth/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 

And finally, my links on my * .cshtml pages will look like

1) @ Html.ActionLink ("Logout", "LogOff", new {area = "Public", controller = "Home"})

or

2) @ Html.ActionLink ("Admin Area", "Index", new {area = "Auth", controller = "Home"})

Hope this saves time on research! By the way, I'm talking about MVC3 here.

Kwex.

+2
Sep 10 '11 at 17:05
source share

This may not be the case for most developers, but I ran into this problem when I added my first area and did not create my solution. As soon as I build my solution, the links began to populate correctly.

+1
May 6 '13 at 9:50
source share



All Articles