Asp.net MVC路由功能

发布于 2024-12-15 08:43:17 字数 720 浏览 2 评论 0原文

有人可以解释一下以下函数的作用吗?我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-12-22 08:43:17

其逻辑位于 System.Web.Mvc.MvcHandler 类、System.Web.Mvc.DefaultControllerFactory 类和 System.Web.Mvc 中.ControllerActionInvoker 类。 .NET Reflector 是您的朋友。

基本上,MVC 框架:

  1. 使用反射来获取应用程序项目中的所有控制器。

  2. 然后它会执行类似 IEnumerable; 的操作controllerNames =controllerTypes.Select(controllerType =>controllerType.Name.Replace("Controller",string.Empty));。然后,它尝试将第一个路径段 {controller} 与这些已清理的控制器类型名称之一(不区分大小写)进行匹配。

  3. 然后,它查看此控制器的公共方法,这些方法的返回类型为 ActionResult 类型或某种派生类型。它将方法名称与第二个路径段 {action} 相匹配,作为要调用的操作方法。

  4. 如果所选方法具有名为 id 的参数,则它将第三个路径段 {id} 与该值相匹配,并将其传递给该方法。否则,可选的id参数将被忽略。

  5. 如果返回的 ActionResult 类型是 ViewResultBase 的派生类,则 IViewEngine 尝试使用任何内容在项目中查找相应的视图已为该视图引擎指定了约定。例如,WebFormViewEngine 在项目中查找 ~/Views/{controller}/{action}.ascx~/Views/{controller}/默认情况下为 {action}.aspx~/Views/Shared/{action}.ascx~/Views/Shared/{action}.aspx

The logic for this is in the System.Web.Mvc.MvcHandler class, the System.Web.Mvc.DefaultControllerFactory class, and the System.Web.Mvc.ControllerActionInvoker class. .NET Reflector is your friend.

Basically, the MVC framework:

  1. Uses reflection to get all the controllers in the application project.

  2. 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).

  3. 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.

  4. 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 optional id parameter is ignored.

  5. If the ActionResult type that is returned is a derivative of ViewResultBase then the IViewEngine tries to locate a corresponding view in the project using whatever conventions have been specified for that view engine. The WebFormViewEngine, 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.

爱她像谁 2024-12-22 08:43:17

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文