ASP.NET MVC 定制 - 继承或更改框架
如果您想更改处理路由的方式,您可以在 MVC 项目中执行此操作或者是否必须更改 MVC 框架?
例如,假设您在 Global.asax.cs 的 RegisterRoutes 中有标准代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
但是假设您真正想要发生的是让您的自定义类拦截所有传入请求并将它们路由到特殊的方式。 这可以通过从 MVC 框架类继承然后自定义来完成,还是必须实际更改框架代码?
If you wanted to alter the way that routes were processed can you do that in a MVC project OR do you have to alter the MVC Framework?
For example lets say you have the standard code in RegisterRoutes of Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
But lets say what you really want to happen is to have your custom class intercept all incoming requests and route them in a special way. Can this be done with via Inheriting from a MVC framework class and then customizing, or do you have to actually alter the framework code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扩展的话,你只需要编写自定义的RouteHandler即可。
请参阅:
ASP.NET MVC,操作 URL 结构
开发自定义 RouteHandler
或网络上的其他内容。
编辑:
您可以通过扩展 RouteBase 并将其添加到路由中来完成更多操作。
Extending, you just need to write custom RouteHandler.
Look at:
ASP.NET MVC, Manipulating URL Structure
Developing Custom RouteHandler
Or planety other on the web.
EDIT:
You can do even more by extending RouteBase and adding it to routes.