这是正确的 MVC 路由设置还是还有其他方法?
我想保留我的网站的根目录用于标准 Web 表单,并将 MVC 页面放在子目录 Views 中,因此我有以下内容。
routes.MapRoute(
"Default", // Route name
"Views/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
但是,尽管这可行并且我看到了一些解决方法,但我不太清楚很高兴 RedirectToAction 似乎将我引导到错误的页面,例如
return RedirectToAction("Index", "Home");
带我到 http://localhost/Views ,这给了我一个未找到的资源,并且 HomeController 上的 Index 操作不会触发。有没有更好的方法来实现我想要的东西,或者我错过了一些明显的东西?
I want to reserve the root of my website to be for standard webforms and have the MVC pages in a subdirectory Views so I have the following..
routes.MapRoute(
"Default", // Route name
"Views/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
However, even though this works and I see a few work-arounds, I'm not quite happy that the RedirectToAction seems to direct me to the wrong page e.g.
return RedirectToAction("Index", "Home");
Takes me to http://localhost/Views
which gives me a resource not found and the Index action on the HomeController doesn't fire. Is there a better way of implementing what I want here or am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如您所知,
Views
是 ASP.NET MVC 中的保留名称。这是一个现有的目录。您可以在路由定义中将RouteExistingFiles
设置为true
:现在,当您导航到
http://example.com/views
或>http://example.com/views/home
或http://example.com/views/home/index
它将是Index
操作将被执行的Home
控制器。As you know
Views
is kind of a reserved name in ASP.NET MVC. It's an existing directory. You could set theRouteExistingFiles
totrue
in your route definitions:Now when you navigate to
http://example.com/views
orhttp://example.com/views/home
orhttp://example.com/views/home/index
it will be theIndex
action ofHome
controller that will get executed.