T4MVC @Url.Action(MVC.Controller.Action()) Renders “?Area=” Parameter in QueryString

谁说我不能喝 提交于 2019-12-17 23:25:05

问题


I am rendering a menu from a Partial Action directly to the layout, using:

@Html.Action(MVC.Menu.Index())

This action, determines which Menu partial to render. For instance, a public menu partial. Within these partials, I am also using T4MVC to render the links:

<ul id="navHolder">
<li class="level1">
    <ul class="mainMenu">
        <li><b>@Html.ActionLink("Welcome", MVC.Home.Index())</b>
           ... 

For some reason, the Urls rendered by T4MVC include "?Area=" at the end:

 <ul id="navHolder">
    <li class="level1">
        <ul class="mainMenu">
            <li><b><a href="/home/index?Area=">Welcome</a></b>
               ...

I have NO areas in my project and I have turned the "IncludeAreasToken" setting to false. Oddly, this only happens if I render the partial using "@Html.Action" -- if I pull it in as "@Html.Partial" the parameter isn't rendered and the link is clean and correct. (I don't want to render it as a partial though, so please don't offer that as a suggestion ;)

Anyone out there run into this before?


回答1:


Something strange is going on here, and I wonder if there is some kind of MVC bug at the root. Even without using T4MVC, this happens if you write:

@Html.ActionLink("Welcome", "Index", "Home", new { Area = "" }, null)

In a regular view, this doesn't generate the bogus ?Area=, while in a Html.Action call it does. I need to ask someone on the team.

For now, you can workaround by deleting this line (around line 310) in t4mvc.tt:

<# if (MvcVersion >= 2) { #>result.RouteValueDictionary.Add("Area", area ?? "");<# } #> 



回答2:


I solve this issue in a very easy way, simply by adding to all routes that are not in area empty area route like this:

routes.MapRoute(
"Default",
"{controller}/{action}/{i​d}",
new { controller = "Home", action = "Index", area = "", id = UrlParameter.Optional });


来源:https://stackoverflow.com/questions/6755729/t4mvc-url-actionmvc-controller-action-renders-area-parameter-in-queryst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!