MVC

1. Routing  : 路由 

  主要是比对通过浏览器传来的http要求与响应适当的网址给浏览器。

@Html.ActionLink("关于","About","Home")

  这段代码生成的HTML超连接:

 <a href="/Home/About">关于</a>

  

2. 默认情况下 网址路由规则定义在 App_StartRouteConfig.cs文档中。

namespace MvcApplication3
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

 1.1 所有的ASP.NET Web 应用程序的路口点都是在 HttpApplication 的Application_Start()中。

  其中 RouteTable.Routes是一个公开的静态对象。用来保存所有的网址路由规则集合 (RouteCollection)。

     在ASP.NET MVC 中 程序会从Global.asax中的 Application_Start() 事件加载一下方法。 

  RouteConfig.RegisterRoutes(RouteTable.Routes); 就是在加载网络路由地址

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

  

1.2 RegisterRoutes 中的IgnoreRoute 方法,是用来定义不要通过网址路由处理的网址,该方法的第一个参数就是设置“不要 通过网址路由处理的URL样式”。

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  所谓的"不要通过网址路由处理的网址" 就是:如果今天客户端浏览器传送过来的网址在这一条规则比对成功,那么就不会再通过网址路由继续比对下去。

1.3 IgnoreRoute 方法中的第一个参数 “resource”。代表RouteValue路由变量,可以取任意值

 {resource}.axd代表所有的*.axd文档网址。所有的.axd文档通常都代表着其中一个HttpHandler .就如.ashx文件一样。

1.4 IgnoreRoute 方法中的第一个参数中还有一个{*pathInfo}参数。也是一个 RouteValue路由变量。

  *pathInfo代表抓取全部的意思。

  完整的 地址 "{resource}.axd/{*pathInfo}"  举个例子:

  若网址为 /WebResource.axd/a/b/c/d 的话, {resource}.axd 就会比对WebResource.axd。

     而{*pathInfo}就会得到啊 a/b/c/d, 如果去掉* 号就只能得到 a.

1.5  MapRoute。是用来定义网址路由扩充的。

    MapRoute方法的指定参数方式 已改为  ”具名参数“ 。

 private void Me(int x, int y = 6, int z = 7)
        { 
            //....
        }
   Me(1, 2, 3);// 标准调用
   Me(1, 5);//  等同与 Me(1,5,7) ,Z已经定义
   Me(1);//也可以

  name :参数第一Route 名称 ,在此为 “Default”.

  url : 具名参数定义。 {controller}/{action}/{id}

    定义URL样式包含三个路由参数分别为controller ,action,id

    如果输入网址 /Home/About/123, 就对应以上三个值 

    Default 具名参数 的默认值,当网址路由比对不到 http要求的网址时会尝试带入这里定义的默认值。

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

  

    

原文地址:https://www.cnblogs.com/dragon-L/p/3737823.html