ASP MVC 区域和 ActionLink
我正在开发一个新项目,我想使用 Phil Haack Areas (1) 的想法 + Steve Sanderson 的调整 (2)。 我有一个简单的根视图,其中包含指向区域视图(Foo)的操作链接。 生成的 URL 具有适当的区域,但它在末尾附加了根控制器 (Bar)。 这是我的根视图代码:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<%= Html.ActionLink("Foo Index Page", "Index", new { area="Foo" } )%>
</asp:Content>
这是它生成的 URL:
localhost: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/
I'm working on a new project where I want to use Phil Haack Areas (1) idea + Steve Sanderson's tweak (2). I have a simple root view with an action link to a view an area (Foo). The URL that is generated has the proper area, but it appends the root controller (Bar) at the end. Here's my root view code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<%= Html.ActionLink("Foo Index Page", "Index", new { area="Foo" } )%>
</asp:Content>
and here's the URL that it generates:
localhost:6494/Foo/Bar
Any idea why the "/Bar" is on there?
(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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了解决方案。 我认为不合适,所以我会要求改进。 通过指定控制器名称,我可以获得正确形成的 URL。 IE
一旦我这样做了,那么 URL 就是正确的
localhost:6494/Foo
为什么这是一个问题? Phil 的演示使用名为 HomeController 的控制器。 我不知道(因为我无法跟踪)Html.ActionLink() 方法如何构建 URL; 但它看起来好像依赖于现有的 HomeController 的默认情况,而我没有。
如果有人对如何允许未命名为 Home 的控制器成为默认控制器有建议,请回复。 谢谢
I've found a solution. I don't think it is appropriate, so I will ask for an improvement. By specifying a controller name, I can get the URL to form properly. I.E.
Once I did this, then the URL was correct
localhost:6494/Foo
Why is this a problem? Phil's demo uses the controller with the name HomeController. I don't know (because I can't trace) how the Html.ActionLink() method goes about constructing the URL; but it looks as if it is relying on a default case of HomeController existing, which I don't have.
If anyone has a suggestion on how to allow for Controllers not named Home to be the default, please reply. Thanks
如果不使用
Home
作为默认控制器名称,只需 更改默认路由。To not have
Home
as the default controller name, simply change the default route.@Joe
在您的 App_Start/RouteConfig.cs 文件中,您必须将默认控制器的值从 Home 更改为您喜欢的任何值:
@Joe
In your App_Start/RouteConfig.cs file you have to change value for default controller from Home to whatever you like :