asp.net-mvc “区域”之间的 ASP.NET MVC `Html.ActionLink`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5432185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC `Html.ActionLink` between "Areas"
提问by jcvandan
I have added a new Area to my MVC3 project and I am trying to link from the _Layout page to the new Area. I have added an Area called 'Admin' that has a controller 'Meets'.
我在我的 MVC3 项目中添加了一个新区域,并且我正在尝试从 _Layout 页面链接到新区域。我添加了一个名为“Admin”的区域,它有一个控制器“Meets”。
I used the visual studio designer to add the area so it has the correct area registration class etc, and the global.asax file is registering all areas.
我使用 Visual Studio 设计器添加区域,使其具有正确的区域注册类等,并且 global.asax 文件正在注册所有区域。
However, when I use the following 2 action links in a page in the root, I run into a few problems:
但是,当我在根页面中使用以下 2 个操作链接时,我遇到了一些问题:
@Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null)
@Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null)
When clicking both links, I am taken to the Meets controller in the Admin area, where the application then proceeds to throw an error saying it cannot find the Index page (even though the Index page is present in the Views folder in the Area sub-directory.
单击这两个链接时,我被带到管理区域中的 Meets 控制器,然后应用程序继续抛出错误,指出它找不到索引页面(即使索引页面存在于区域子的视图文件夹中)目录。
The href for the 1st link looks like this:
第一个链接的 href 如下所示:
http://localhost/BCC/Meets?area=Admin
http://localhost/BCC/Meets?area=Admin
And the href for the 2nd link looks like this:
第二个链接的 href 如下所示:
http://localhost/BCC/Meets
http://localhost/BCC/Meets
Also if I hit the link that I expect to be created:
此外,如果我点击了我希望创建的链接:
http://localhost/BCC/Admin/Meets
http://localhost/BCC/Admin/Meets
I just get a resource cannot be found error. All very perplexing! I hope someone can help...
我刚收到一个资源找不到错误。都非常令人费解!我希望有人能帮帮忙...
采纳答案by jcvandan
I figured this out - I created a new test project and did exactly the same thing I was doing before and it worked...then after further inspection of all things route-related between the two projects I found a discrepancy.
我想通了 - 我创建了一个新的测试项目,并做了与我之前做的完全相同的事情并且它起作用了......然后在进一步检查两个项目之间与路线相关的所有事情后,我发现了差异。
In the global.asax file in my BCC application, there was a rogue line of code which had inexplicably appeared:
在我的 BCC 应用程序的 global.asax 文件中,莫名其妙地出现了一行流氓代码:
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 time or other I had placed the routes.Clear() call at the beginning of RegisterRoutes, which meant after I had registered the Areas in Application_Start, I was then immediately clearing what I had just registered.
正如你所看到的,我在某个时候或其他时候在 RegisterRoutes 的开头放置了 routes.Clear() 调用,这意味着在我在 Application_Start 中注册了区域之后,我立即清除了我刚刚拥有的挂号的。
Thanks for the help...it did ultimately lead to my salvation!
感谢您的帮助......它最终导致了我的救赎!
回答by Darin Dimitrov
Strange indeed. Steps that worked perfectly fine for me:
确实奇怪。对我来说非常有效的步骤:
- Create a new ASP.NET MVC 3 application using the default Visual Studio template
- Add an area called
Admin
using Visual Studio designer by right clicking on the project Add new Controller in
~/Areas/Admin/Controllers/MeetsController
:public class MeetsController : Controller { public ActionResult Index() { return View(); } }
Add a corresponding view
~/Areas/Admin/Views/Meets/Index.cshtml
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)
- Run the application.
- 使用默认的 Visual Studio 模板创建一个新的 ASP.NET MVC 3 应用程序
Admin
通过右键单击项目添加使用 Visual Studio 设计器调用的区域添加新控制器
~/Areas/Admin/Controllers/MeetsController
:public class MeetsController : Controller { public ActionResult Index() { return View(); } }
添加相应的视图
~/Areas/Admin/Views/Meets/Index.cshtml
在布局 (
~/Views/Shared/_Layout.cshtml
) 中添加链接:@Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null) @Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null)
- 运行应用程序。
Rendered HTML for the anchors:
为锚点渲染的 HTML:
<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?
那么你的设置有什么不同呢?
回答by Shaun Wilson
Another option is to utilize RouteLink() instead of ActionLink(), which bypasses the area registrations altogether:
另一种选择是使用 RouteLink() 而不是 ActionLink(),它完全绕过区域注册:
ActionLink version:
动作链接版本:
Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }, null)
RouteLink version:
RouteLink 版本:
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.
第二个参数是在 Global.asax.cs 和各种“AreaRegistration”子类中注册的“路由名称”。使用'RouteLink'连接不同区域,只需要指定正确的路由名称即可。
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!
快乐编码!
回答by Mikael ?stberg
Verify that your AdminAreaRegistration
class looks like this:
验证您的AdminAreaRegistration
类是否如下所示:
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 that you have this in Global.asax.cs
:
并且你有这个Global.asax.cs
:
protected void Application_Start()
{
... // ViewEngine Registration
AreaRegistration.RegisterAllAreas();
... // Other route registration
}
回答by Kwex
I solved this problem by doing the following.
我通过执行以下操作解决了这个问题。
In my Global.asax.cs, I have
在我的 Global.asax.cs 中,我有
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've got
在我的 PublicAreaRegistration.cs(公共区域)中,我有
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've got
在我的 AuthAreaRegistration.cs(限制访问区域)中,我有
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 in my *.cshtml pages would be like
最后,我在 *.cshtml 页面中的链接就像
1) @Html.ActionLink("Log Off", "LogOff", new{area= "Public", controller="Home"})
1) @Html.ActionLink("注销", "注销", new{area="Public", controller="Home"})
or
或者
2) @Html.ActionLink("Admin Area", "Index", new {area= "Auth", controller="Home"})
2) @Html.ActionLink("Admin Area", "Index", new {area="Auth", controller="Home"})
Hope this saves someone hours of research! BTW, I'm talking about MVC3 here.
希望这可以节省一些人的研究时间!顺便说一句,我在这里谈论的是 MVC3。
Kwex.
奎克斯。
回答by Abhi
This might not be the case for most of the developers but I encountered this problem when I added a my first area and did not build my solution. As soon as I build my solution the links started to populate correctly.
对于大多数开发人员来说可能不是这种情况,但是当我添加第一个区域并且没有构建我的解决方案时,我遇到了这个问题。一旦我构建了我的解决方案,链接就开始正确填充。