“区域”之间的 ASP.NET MVC `Html.ActionLink`
我已向我的 MVC3 项目添加了一个新区域,并且我正在尝试从 _Layout 页面链接到新区域。我添加了一个名为“Admin”的区域,其中有一个控制器“Meets”。
我使用 Visual Studio 设计器添加区域,以便它具有正确的区域注册类等,并且 global.asax 文件正在注册所有区域。
但是,当我在根目录的页面中使用以下 2 个操作链接时,我遇到了一些问题:
@Html.ActionLink("Admin", "Index", "Meets", new { area = "Admin" }, null)
@Html.ActionLink("Admin", "Index", "Meets", new { area = "" }, null)
单击这两个链接时,我将被带到管理区域中的 Meets 控制器,然后应用程序将在其中引发错误说它找不到索引页面(即使索引页面存在于 Area 子目录的 Views 文件夹中。
第一个链接的 href 如下所示:
http://localhost/BCC/Meets?area =Admin
第二个链接的 href 如下所示:
http://localhost/BCC/Meets
另外,如果我点击我期望创建的链接:
http ://localhost/BCC/Admin/Meets
我刚刚收到一个资源无法找到的错误,我希望有人可以帮助...
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'.
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.
However, when I use the following 2 action links in a page in the root, I run into a few problems:
@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.
The href for the 1st link looks like this:
http://localhost/BCC/Meets?area=Admin
And the href for the 2nd link looks like this:
http://localhost/BCC/Meets
Also if I hit the link that I expect to be created:
http://localhost/BCC/Admin/Meets
I just get a resource cannot be found error. All very perplexing! I hope someone can help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我通过执行以下操作解决了这个问题。
在我的 Global.asax.cs 中,我有
在我的 PublicAreaRegistration.cs(公共区域)中,我有
在我的 AuthAreaRegistration.cs(限制访问区域)中,我有
最后,我的 *.cshtml 中的链接页面将类似于
1) @Html.ActionLink("Log Off", "LogOff", new{area= "Public",controller="Home"})
或
2) @Html.ActionLink("Admin Area", "Index ", new {area= "Auth",controller="Home"})
希望这可以节省人们的研究时间!顺便说一句,我在这里谈论的是 MVC3。
奎克斯。
I solved this problem by doing the following.
In my Global.asax.cs, I have
In my PublicAreaRegistration.cs (Public Area), I've got
In my AuthAreaRegistration.cs (Area for Restricted access), I've got
And finally, my links in my *.cshtml pages would be like
1) @Html.ActionLink("Log Off", "LogOff", new{area= "Public", controller="Home"})
or
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.
Kwex.
对于大多数开发人员来说,情况可能并非如此,但当我添加第一个区域并且没有构建我的解决方案时,我遇到了这个问题。一旦我构建了解决方案,链接就开始正确填充。
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.
确实很奇怪。对我来说非常有效的步骤:
添加新的, 使用 Visual Studio 设计器添加一个名为
Admin
的区域~/Areas/Admin/Controllers/MeetsController
中的控制器:添加相应的视图
~/Areas/Admin/Views/Meets/Index .cshtml
在布局中(
~/Views/Shared/ _Layout.cshtml
)添加链接:锚点的渲染 HTML:
正如预期的那样,第一个链接有效,而第二个链接无效。
那么和你的设置有什么区别呢?
Strange indeed. Steps that worked perfectly fine for me:
Admin
using Visual Studio designer by right clicking on the projectAdd new Controller in
~/Areas/Admin/Controllers/MeetsController
:Add a corresponding view
~/Areas/Admin/Views/Meets/Index.cshtml
In the layout (
~/Views/Shared/_Layout.cshtml
) add links:Rendered HTML for the anchors:
As expected the first link works whereas the second doesn't.
So what's the difference with your setup?
另一种选择是使用 RouteLink() 而不是 ActionLink(),它完全绕过区域注册:
ActionLink 版本:
RouteLink 版本:
第二个参数是“路由名称”,在 Global.asax.cs 和各种“AreaRegistration”中注册' 子类。要使用“RouteLink”连接不同区域,您只需指定正确的路线名称即可。
下面的示例显示了我如何从共享部分生成到不同区域的三个链接,无论我位于哪个区域(如果有),该链接都可以正常工作:
快乐编码!
Another option is to utilize RouteLink() instead of ActionLink(), which bypasses the area registrations altogether:
ActionLink version:
RouteLink version:
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.
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):
Happy coding!
我发现了这一点 - 我创建了一个新的测试项目,并做了与我之前所做的完全相同的事情,并且它有效......然后在进一步检查两个项目之间与路线相关的所有内容后,我发现了差异。
在我的 BCC 应用程序的 global.asax 文件中,有一行莫名其妙地出现的恶意代码:
正如您所看到的,我的注释在哪里,有时我在开头放置了 paths.Clear() 调用RegisterRoutes,这意味着当我在Application_Start中注册了Areas之后,我立即清除了我刚刚注册的内容。
感谢您的帮助...它最终导致了我的救赎!
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:
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.
Thanks for the help...it did ultimately lead to my salvation!
验证您的 AdminAreaRegistration 类是否如下所示:
并且您在 Global.asax.cs 中包含该类:
Verify that your
AdminAreaRegistration
class looks like this:and that you have this in
Global.asax.cs
: