Asp.net MVC路由功能
有人可以解释一下以下函数的作用吗?我正在学习 Asp.net MVC,无法理解何时调用哪个控制器并呈现哪个视图。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//register custom routes (plugins, etc)
var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
routePublisher.RegisterRoutes(routes);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
}
此代码来自 nopCommerce 源代码。我无法理解这个项目的 URL 路由
Can someone please explain what the following function does. I am learning Asp.net MVC and unable to understand which controller is called when and renders which view.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//register custom routes (plugins, etc)
var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
routePublisher.RegisterRoutes(routes);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
}
This code is from nopCommerce source-code. I can't understand the URL routing for this project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其逻辑位于 System.Web.Mvc.MvcHandler 类、System.Web.Mvc.DefaultControllerFactory 类和 System.Web.Mvc 中.ControllerActionInvoker 类。 .NET Reflector 是您的朋友。
基本上,MVC 框架:
使用反射来获取应用程序项目中的所有控制器。
然后它会执行类似
IEnumerable; 的操作controllerNames =controllerTypes.Select(controllerType =>controllerType.Name.Replace("Controller",string.Empty));
。然后,它尝试将第一个路径段{controller}
与这些已清理的控制器类型名称之一(不区分大小写)进行匹配。然后,它查看此控制器的公共方法,这些方法的返回类型为
ActionResult
类型或某种派生类型。它将方法名称与第二个路径段{action}
相匹配,作为要调用的操作方法。如果所选方法具有名为
id
的参数,则它将第三个路径段{id}
与该值相匹配,并将其传递给该方法。否则,可选的id
参数将被忽略。如果返回的
ActionResult
类型是ViewResultBase
的派生类,则IViewEngine
尝试使用任何内容在项目中查找相应的视图已为该视图引擎指定了约定。例如,WebFormViewEngine
在项目中查找~/Views/{controller}/{action}.ascx
、~/Views/{controller}/默认情况下为 {action}.aspx
、~/Views/Shared/{action}.ascx
、~/Views/Shared/{action}.aspx
。如果您想进一步了解路由在 MVC 中的工作原理,我强烈建议 Scott Gu 关于 MVC 路由的文章。
就
IRoutePublisher
方法而言,它看起来像是一个nopCommerce
特定方法,会自动注册特定于nopCommerce
的其他路由。的配置。如果您对 nopCommerce 的特定路由约定如何工作感兴趣,您可以从 nopCommerce codeplex 页面下载源代码并搜索其默认的IRoutePublisher
实现。IRoutePublisher
位于此处:http://nopcommerce.codeplex.com/SourceControl/changeset/view/7e34dd9d98f3#src%2fPresentation%2fNop.Web.Framework%2fMvc%2fRoutes%2fRoutePublisher.cs 。基本上,它获取IRouteProvider
的所有实现,并根据优先级按顺序注册它们的路由定义。The logic for this is in the
System.Web.Mvc.MvcHandler
class, theSystem.Web.Mvc.DefaultControllerFactory
class, and theSystem.Web.Mvc.ControllerActionInvoker
class. .NET Reflector is your friend.Basically, the MVC framework:
Uses reflection to get all the controllers in the application project.
Then it does something like
IEnumerable<string> controllerNames = controllerTypes.Select(controllerType => controllerType.Name.Replace("Controller",string.Empty));
. It then tries to match the first path segment,{controller}
, to one of these sanitized controller type names (case-insensitive).Then, it looks at this controller's public methods that have a return type that is of type
ActionResult
or some derivative. It matches the method name to the second path segment,{action}
, as the action method to be called.If the selected method has a parameter that is named
id
, then it matches the third path segment{id}
to that value, and passes it to the method. Otherwise, the optionalid
parameter is ignored.If the
ActionResult
type that is returned is a derivative ofViewResultBase
then theIViewEngine
tries to locate a corresponding view in the project using whatever conventions have been specified for that view engine. TheWebFormViewEngine
, for example, looks in the project for~/Views/{controller}/{action}.ascx
,~/Views/{controller}/{action}.aspx
,~/Views/Shared/{action}.ascx
,~/Views/Shared/{action}.aspx
by default.If you want to further understand how routing works in MVC, I would highly suggest Scott Gu's article on MVC Routing.
As far as the
IRoutePublisher
method, that looks like anopCommerce
specific method that automatically registers additional routes specific tonopCommerce
's configuration. If you are interested in how nopCommerce's specific routing conventions work, you can download the source code from the nopCommerce codeplex page and do a search for its defaultIRoutePublisher
implementation.IRoutePublisher
is here: http://nopcommerce.codeplex.com/SourceControl/changeset/view/7e34dd9d98f3#src%2fPresentation%2fNop.Web.Framework%2fMvc%2fRoutes%2fRoutePublisher.cs . Basically, it gets all implementations ofIRouteProvider
and registers their route definitions in order according to their priority.nopCommerce 采用松散耦合的基础设施,分别为每个插件注册路由。
因此,如果您需要了解发生了什么,请检查 nopCommerce 源代码并查找每个插件都有的 RouteProvider 类。它们在应用程序启动时动态加载。
如果您需要创建自己的路线,您仍然可以按照传统方式进行 - 但请注意,可能会发生一些冲突。
(免责声明:我只是查看了源代码,不知道其他任何内容)。
nopCommerce employs a loosely coupled infrastructure that registers routes for each plugin separately.
So If you need to understand what's going on, check the nopCommerce source code and look for RouteProvider classes, that each plugin has. They are dynamically loaded on application start.
If you need to create your own routes, you can still do that the traditional way -- but be aware, that there might be some clashes.
(Disclaimer: I just looked at the source code, don't know anything else about it).