ASP.NET MVC 路由根级视图
我以为这会很容易,但我完全感到困惑。
我希望一个控制器的视图位于应用程序的根级别,而不是位于该控制器的子目录中,但我无法弄清楚。
我想要这两个网址:
/Info - 这应该是控制器“Home”上的操作“Info”
/Admin/ - 这应该是控制器“Admin”上的操作“Index”(默认)
到目前为止,无论我做什么我已经尝试过,第一条路线最终会捕获两条路线。 我似乎无法将两者分开。
该信息页面甚至不需要控制器,它是静态的,但我确实想使用母版页。 可能有一种更简单的方法可以实现这一点,但我也还没有弄清楚。
我能想到的所有方法都是创建一个 Info 控制器,并将 Views/Home/Info 移动到 Views/Info/Index ,但这有一定的味道。
我能够使用以下方法在 Rails 中执行此操作:
map.connect ':controller/:action/:id'
map.connect ':action', :controller => 'home'
I thought this would be fairly easy, but I'm totally baffled.
I want one controller's views to be at the root level of the application, rather than in a subdirectory for that controller, but I cannot figure it out.
I'd like to have these two urls:
/Info - This should action "Info" on controller "Home"
/Admin/ - This should be action "Index" (default) on controller "Admin"
So far no matter what I've tried, the first route will end up catching both. I can't seem to separate the two.
That Info page doesn't even need a controller, it' static, but I do want to use a master page. There may be a much easier way to pull this off, but I haven't figured that out either.
All I can think of that would work, would be to create an Info controller, and move Views/Home/Info to Views/Info/Index, but that has a certain smell to it.
I was able to do this in rails using:
map.connect ':controller/:action/:id'
map.connect ':action', :controller => 'home'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只需要正确的路线。 对于您的情况:
但我建议您这种方法。
如果您需要更改视图/部分视图的默认物理位置,
查看如何创建自定义视图引擎。
You just need proper routes. In your case:
But i recommend you this approach.
If you need to change default physical location of views/partialviews,
check out how to create custom view engines.
您可以使用路由属性。
在你的路由配置文件中你应该有。
然后在每个操作之上你可以有一个路由属性。
您甚至可以通过添加参数和/或子文件夹来更高级地使用这些属性。
您可以将上述属性放在任何操作上,它看起来就像来自博客控制器一样。 但是,您甚至不需要博客控制器。 此外,{} 表示参数,因此只需确保您的操作采用与大括号中的参数相同的参数即可。 在这种情况下,参数将是
You can use Route Attributes.
In your route config file you should have.
Then above every action you can have a route attribute.
You can even get more advanced with these attributes by adding parameters, and/or subfolders
You can put the above attribute on any action, and it will appear as if it's originating from the blog controller. However, you don't even need a blog controller. Also the {} signify the parameter, so just make sure your action is taking the same parameter as what's in the curly braces. In this case the parameter would be