MVC 路由怪异

发布于 2024-10-18 06:54:44 字数 1204 浏览 5 评论 0原文

大家好,对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甩你一脸翔 2024-10-25 06:54:44

如果博客位于与其他区域不同的区域中,则 MVC 预计来自该区域的链接位于同一区域内,因此它将该区域附加到您的 URL。如果它们位于不同的区域,您需要使用“区域”路由值调用 ActionLink。例如,如果“约会”位于“社交”区域,您可以使用:

@Html.ActionLink("Dating", "Index", new { controller = "Dating", area = "Social" } );

以下是来自 ASP.NET MVC 2 中的新增功能

“Area”现在是一个保留的路由值键

现在路线值中的字符串“area”
在 ASP.NET MVC 中有特殊含义,在
与“控制器”相同的方式
“行动”做。其含义之一是
如果 HTML 助手提供了
路由值字典包含
“区域”,帮手将不再
在查询字符串中附加“area”。

如果您使用区域功能,
确保不要使用 {area} 作为
您的路线网址。

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:

@Html.ActionLink("Dating", "Index", new { controller = "Dating", area = "Social" } );

Here is the relevant discussion from What’s New in ASP.NET MVC 2:

“Area” is a now a reserved route-value key

The string “area” in Route values now
has special meaning in ASP.NET MVC, in
the same way that “controller” and
“action” do. One implication is that
if HTML helpers are supplied with a
route-value dictionary containing
“area”, the helpers will no longer
append “area” in the query string.

If you are using the Areas feature,
make sure to not use {area} as part of
your route URL.

梦在深巷 2024-10-25 06:54:44

它从当前上下文中获取未定义的值。例如 @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 on Foo 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 controller Foo action Test with no area.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文