T4MVC @Url.Action(MVC.Controller.Action()) 渲染“?Area=”查询字符串中的参数

发布于 2024-11-25 10:05:48 字数 814 浏览 3 评论 0原文

我将部分操作中的菜单直接渲染到布局,使用:

@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 技术交流群。

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

发布评论

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

评论(3

梦幻的味道 2024-12-02 10:05:48

我以一种非常简单的方式解决这个问题,只需添加到不在区域空区域路线中的所有路线,如下所示:

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

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 });
巾帼英雄 2024-12-02 10:05:48

这里发生了一些奇怪的事情,我想知道是否在根源上存在某种 MVC bug。即使不使用 T4MVC,如果您编写以下内容,也会发生这种情况:

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

在常规视图中,这不会生成虚假的 ?Area=,而在 Html.Action 调用中它会生成。我需要询问团队中的某个人。

目前,您可以通过删除 t4mvc.tt 中的这一行(第 310 行左右)来解决此问题:

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

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 ?? "");<# } #> 
迷迭香的记忆 2024-12-02 10:05:48

从workitem 7154评论中复制的是@DavidEbbo提供的解决方案:

一个更简单的解决方法是向您的网站添加一个虚假区域。例如\n\n-
右键单击项目并选择添加/区域。将其命名为“虚拟”(或
无论如何)\n- 你可以删除其中的所有内容,除了
DummyAreaRegistration.cs 文件

确保您的 Global.asax 中有 AreaRegistration.RegisterAllAreas(); 调用

这也适用于适当的属性路由。

Copied from workitem 7154 comments is a solution provided by @DavidEbbo:

A simpler workaround is to add a bogus area to your site. e.g.\n\n-
Right click Project and choose Add / Area. Name it 'Dummy' (or
whatever)\n- You can delete everything in there except for the
DummyAreaRegistration.cs file

Make sure you have the AreaRegistration.RegisterAllAreas(); call in your Global.asax

This also works with attribute routing in place.

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