MVC 路由怪异
大家好,对于 ASP.NET MVC 来说是全新的,我正在通过构建现有的 MVC 模板来创建一个假社交网站(用于学习目的)...我添加了一些视图等,这些都工作得很好。但是,现在我添加了一个名为“博客”的 mvc 区域,并将链接添加到主菜单。现在,如果我单击任何菜单项,一切都会按预期工作 - 但是,当我单击“博客”菜单项时,视图等会显示博客页面,但是其他视图的菜单链接前面有 /Blog/现在的网址!?不确定我是否做错了什么......这是我的菜单代码:
<div id="menucontainer">
<ul id="menu">
@* @Html.ActionLink() Params = String Name, String Controller Name,
string Method (actionLink) Name *@
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Mail", "Index", "Mail")</li>
<li>@Html.ActionLink("Search", "Index", "Search")</li>
<li>@Html.ActionLink("Dating", "Index", "Dating")</li>
<li>@Html.ActionLink("Groups", "Index", "Groups")</li>
<li>@Html.ActionLink("Forums", "Index", "Board")</li>
<li>@Html.ActionLink("Blog", "Index", "Blog")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
Hey all, brand new to asp.net mvc and I am creating a fake social site (for learning purposes) by building off the stock mvc template... I added some views, etc. which all work fine. However, now I have added an mvc area called "Blog" and added the link to the main menu. Now if I click on any of the menu items, things work as expected - however when I click on the "Blog" menu item the view, etc show the blog page however the menus links for the other views have the /Blog/ in front of the URL now!? Not sure if I'm doing something wrong... here is my menu code:
<div id="menucontainer">
<ul id="menu">
@* @Html.ActionLink() Params = String Name, String Controller Name,
string Method (actionLink) Name *@
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Mail", "Index", "Mail")</li>
<li>@Html.ActionLink("Search", "Index", "Search")</li>
<li>@Html.ActionLink("Dating", "Index", "Dating")</li>
<li>@Html.ActionLink("Groups", "Index", "Groups")</li>
<li>@Html.ActionLink("Forums", "Index", "Board")</li>
<li>@Html.ActionLink("Blog", "Index", "Blog")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果博客位于与其他区域不同的区域中,则 MVC 预计来自该区域的链接位于同一区域内,因此它将该区域附加到您的 URL。如果它们位于不同的区域,您需要使用“区域”路由值调用 ActionLink。例如,如果“约会”位于“社交”区域,您可以使用:
以下是来自 ASP.NET MVC 2 中的新增功能:
If Blog is in a separate Area from the others, MVC expects links from that area to be within the same Area, so it appends the Area to your URL. If they are in a different Area, you need to invoke ActionLink with an "Area" route value. For instance, if "Dating" is in the "Social" area, you might use:
Here is the relevant discussion from What’s New in ASP.NET MVC 2:
它从当前上下文中获取未定义的值。例如
@Html.ActionLink("Test", "Test")
将创建相对于控制器的链接。例如,如果您将在Foo
控制器上渲染它,它将渲染元素Test
。区域也是如此 - 如果您想要跨区域的静态链接,您将必须定义它应该引导您到哪个区域。示例:@Html.ActionLink("Test", "Test", "Foo", new { @area = string.Empty }, null)
将始终链接到控制器Foo
没有区域的操作测试
。It takes undefined values from current context. For example
@Html.ActionLink("Test", "Test")
will create link relative to controller. So for example if you will render this onFoo
controller it will render element<a href="/Foo/Test">Test</a>
. Same goes for area - if you want static link across areas, you will have to define to which area it should guide you. Example:@Html.ActionLink("Test", "Test", "Foo", new { @area = string.Empty }, null)
will always link to controllerFoo
actionTest
with no area.