Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net MVC路由功能_C#_Asp.net Mvc 3_Nopcommerce - Fatal编程技术网

C# Asp.net MVC路由功能

C# Asp.net MVC路由功能,c#,asp.net-mvc-3,nopcommerce,C#,Asp.net Mvc 3,Nopcommerce,有人能解释一下以下函数的作用吗。我正在学习Asp.net MVC,无法理解何时调用哪个控制器以及呈现哪个视图 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //register custom routes (plugins, etc) var routePub

有人能解释一下以下函数的作用吗。我正在学习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" }
        );
    }
publicstaticvoidregisterOutes(路由收集路由)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
//注册自定义路由(插件等)
var routePublisher=EngineContext.Current.Resolve();
routePublisher.RegisterRoutes(路线);
routes.MapRoute(
“默认值”,//路由名称
“{controller}/{action}/{id}”,//带参数的URL
新建{controller=“Home”,action=“Index”,id=UrlParameter.Optional},
新[]{“Nop.Web.Controllers”}
);
}

此代码来自nopCommerce源代码。我无法理解这个项目的URL路由

nopCommerce使用了一个松散耦合的基础设施,分别为每个插件注册路由

因此,如果您需要了解发生了什么,请检查nopCommerce源代码并查找每个插件都具有的RouteProvider类。它们在应用程序启动时动态加载

如果您需要创建自己的路线,您仍然可以使用传统的方法来创建,但是请注意,可能会有一些冲突


(免责声明:我只是查看了源代码,对它一无所知)。

这方面的逻辑在
System.Web.Mvc.MvcHandler
类、
System.Web.Mvc.DefaultControllerFactory
类和
System.Web.Mvc.ControllerActionInvoker
类中。他是你的朋友

基本上,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中的路由是如何工作的,我强烈建议您

    • 至于
      iroutpublisher
      方法,它看起来像是一个
      nopCommerce
      特定的方法,可以自动注册特定于
      nopCommerce
      配置的其他路由。如果您对nopCommerce的特定路由约定的工作方式感兴趣,可以从下载源代码并搜索其默认的
      IRoutePublisher
      实现

      • 更新默认的
        IRoutePublisher
        在这里:。基本上,它获取
        IRouteProvider
        的所有实现,并根据优先级顺序注册它们的路由定义
      • 默认路由提供程序为:和

  • 看起来大多数有趣的路线都是由“路线发布者”添加的,但没有显示。不知道我们如何回答这个问题。@MarcGravel,我在哪里可以找到你说的这条路线??如果不知道这一点,我无法理解为哪个URLsee routePublisher.RegisterRoutes调用哪个控制器-除此之外。。。如果这是一个产品,请参阅文档!