如何在 ASP.NET MVC 中拥有通用路由,但处理未知操作?
如果我有一条路线:
routes.MapRoute(
"RouteName", // route name
"{action}", // url with parameters
new { controller = "Home", action = "Index", id = "" } // parameter defaults
);
我希望能够捕获以下形式的 URL:
http://sitename.com/about
http://sitename.com/contact
http://sitename.com/others
当 Home 控制器中存在带有所需 URL 名称的操作时,这显然可以工作。如果我输入错误的 URL,例如 http://sitename.com/foo,并且 foo 操作会执行Home 控制器中不存在,我想将应用程序定向到未找到的 404 页面,但显然如果我输入 foo,它会在 Home 控制器中寻找 foo 操作。有没有办法保持通用而不将所有子页面硬编码到 global.asax 中。如果可能的话,我想避免:
routes.MapRoute(
"About", // route name
"about", // url with parameters
new { controller = "Home", action = "About", id = "" } // parameter defaults
);
routes.MapRoute(
"Contact", // route name
"contact", // url with parameters
new { controller = "Home", action = "Contact", id = "" } // parameter defaults
);
谢谢。
If I have a route:
routes.MapRoute(
"RouteName", // route name
"{action}", // url with parameters
new { controller = "Home", action = "Index", id = "" } // parameter defaults
);
I want to be able to catch URL's of the form:
http://sitename.com/about
http://sitename.com/contact
http://sitename.com/others
This obviously works when an action exists within the Home controller that carries the name of the desired URL. If I enter an erroneous URL, such as http://sitename.com/foo, and the foo action does not exist within the Home controller, I would like to direct the application to a 404 page not found, but obviously if I enter foo, it is looking for the foo action within the home controller. Is there any way to remain generic without hardcoding all the subpages into the global.asax. If at all possible, I want to refrain from:
routes.MapRoute(
"About", // route name
"about", // url with parameters
new { controller = "Home", action = "About", id = "" } // parameter defaults
);
routes.MapRoute(
"Contact", // route name
"contact", // url with parameters
new { controller = "Home", action = "Contact", id = "" } // parameter defaults
);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过对单个路线使用约束?这将允许您匹配顶层家庭(或其他)控制器上存在的任何操作。
其中 MatchesHomeControllerConstraint 是:
Have you tried using a constraint with a single route? This would allow you to match any action that exists on your Home (or other) controller at the top level.
Where MatchesHomeControllerConstraint is:
也许我没有正确理解你的问题,但是你问题顶部的路线已经涵盖了你在底部输入的情况。没有必要像这样明确地定义它们。如果他们输入不存在的操作,则会抛出 404 错误。
如果您想在某人匹配有效控制器但未提供有效操作时执行一些特殊操作,您可以重写控制器中的
HandleUnknownError
。Maybe I'm not understanding your question correctly, but the route that you have at the top of your question will already cover the cases you're typing out at the bottom. There is no need to define them explicitly like that. If they type in an action that doesn't exist, a 404 error will be thrown.
If you want to do something special when someone matches a valid controller but doesn't supply a valid action, you can override
HandleUnknownError
in your controller.