开发者

MVC Route Weirdness

开发者 https://www.devze.com 2023-02-11 04:57 出处:网络
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 hav

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&开发者_C百科gt;
            </div>


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号