gpt4 book ai didi

asp.net-mvc - ASP MVC 区域和 ActionLink

转载 作者:行者123 更新时间:2023-12-04 04:24:33 25 4
gpt4 key购买 nike

我正在开发一个新项目,我想在其中使用 Phil Haack Areas (1) 的想法 + Steve Sanderson 的调整 (2)。我有一个简单的根 View ,带有一个指向某个区域 (Foo) 的操作链接。生成的 URL 具有适当的区域,但它在末尾附加了根 Controller (Bar)。这是我的根 View 代码:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

<%= Html.ActionLink("Foo Index Page", "Index", new { area="Foo" } )%>

</asp:Content>

这是它生成的 URL:

本地主机:6494/Foo/Bar

知道为什么“/Bar”在那里吗?

(1): haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

(2): blog.codeville.net/2008/11/05/app-areas-in-aspnet-mvc-take-2/

最佳答案

我找到了解决办法。我认为不合适,所以我会要求改进。通过指定 Controller 名称,我可以获得正确形成的 URL。 IE。

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

<%= Html.ActionLink("Foo Index Page", "Index", new { area="Foo", controller="Baz" } )%>

</asp:Content>

一旦我这样做了,那么 URL 是正确的

本地主机:6494/Foo

为什么这是个问题? Phil 的演示使用名为 HomeController 的 Controller 。我不知道(因为我无法追踪)Html.ActionLink() 方法是如何构建 URL 的;但它看起来好像依赖于现有的 HomeController 的默认情况,而我没有。

如果有人对如何允许未命名为 Home 的 Controller 作为默认 Controller 有任何建议,请回复。谢谢

关于asp.net-mvc - ASP MVC 区域和 ActionLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/883244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com