T4MVC @Url.Action(MVC.Controller.Action()) 渲染“?Area=”查询字符串中的参数
我将部分操作中的菜单直接渲染到布局,使用:
@Html.Action(MVC.Menu.Index())
此操作确定要渲染哪个菜单部分。例如,公共菜单部分。在这些部分中,我还使用 T4MVC 渲染链接:
<ul id="navHolder">
<li class="level1">
<ul class="mainMenu">
<li><b>@Html.ActionLink("Welcome", MVC.Home.Index())</b>
...
出于某种原因,T4MVC 渲染的 URL 末尾包含“?Area=”:
<ul id="navHolder">
<li class="level1">
<ul class="mainMenu">
<li><b><a href="/home/index?Area=">Welcome</a></b>
...
我的项目中没有区域,并且我已将“IncludeAreasToken”设置设置为错误的。奇怪的是,只有当我使用“@Html.Action”渲染部分时才会发生这种情况 - 如果我将其作为“@Html.Partial”拉入,则不会渲染参数并且链接是干净且正确的。 (虽然我不想将其呈现为部分内容,所以请不要将其作为建议提供;)
之前有人遇到过这个吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以一种非常简单的方式解决这个问题,只需添加到不在区域空区域路线中的所有路线,如下所示:
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:
这里发生了一些奇怪的事情,我想知道是否在根源上存在某种 MVC bug。即使不使用 T4MVC,如果您编写以下内容,也会发生这种情况:
在常规视图中,这不会生成虚假的 ?Area=,而在 Html.Action 调用中它会生成。我需要询问团队中的某个人。
目前,您可以通过删除 t4mvc.tt 中的这一行(第 310 行左右)来解决此问题:
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:
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:
从workitem 7154评论中复制的是@DavidEbbo提供的解决方案:
确保您的 Global.asax 中有
AreaRegistration.RegisterAllAreas();
调用这也适用于适当的属性路由。
Copied from workitem 7154 comments is a solution provided by @DavidEbbo:
Make sure you have the
AreaRegistration.RegisterAllAreas();
call in your Global.asaxThis also works with attribute routing in place.