MVC中Global的Application_Start方法详解

Global文件
Application_Start方法中包含注册区域、注册全局的Filter、注册路由、合并压缩、打包工具Combres
        private Logger logger = new Logger(typeof(MvcApplication));
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();//注册区域
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);//注册全局的Filter
            RouteConfig.RegisterRoutes(RouteTable.Routes);//注册路由
            BundleConfig.RegisterBundles(BundleTable.Bundles);//合并压缩 ,打包工具 Combres

            this.logger.Info("网站启动了。。。");
        }

下面看下注册全局Filter的方法,创建MVC项目时会创建此文件,位于App_Start文件夹下

    /// <summary>
    /// 路由:映射,按照规则转发
    /// http://localhost:2017/Second/index
    /// 从Second/index开始匹配
    /// 
    /// 路由是按照注册顺序进行匹配,遇到第一个吻合的就结束匹配;每个请求只会被一个路由匹配上
    /// </summary>
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }

下面看下注册路由的方法,创建MVC项目时会创建此文件,位于App_Start文件夹下

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //忽略路由  正则表达式  {resource}表示变量   a.axd/xxxx   resource=a   pathInfo=xxxx
            //.axd是历史原因,最开始都是webform,请求都是.aspx后缀,IIS根据后缀转发请求;MVC出现了,没有后缀,IIS6以及更早版本,打了个补丁,把mvc的请求加上个.axd的后缀,然后这种都转发到网站----新版本的IIS已经不需要了,遇到了就直接忽略,还是走原始流程
            routes.IgnoreRoute("CustomService/{*pathInfo}");//以CustomService开头,都不走路由

            routes.MapRoute(
                name: "About",
                url: "About",
                defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
                );//固定路由,/Home/About----About

            routes.MapRoute(
               name: "Test",
               url: "Test/{action}/{id}",
               defaults: new { controller = "Second", action = "Index", id = UrlParameter.Optional }
               );//修改控制器,

            routes.MapRoute(
              name: "Regex",
              url: "{controller}/{action}_{year}_{month}_{day}",
              defaults: new { controller = "Second", action = "Index", id = UrlParameter.Optional },
              constraints: new { year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" }//constraints约束
              );
            //http://localhost:2017/second/Time_2019_06_13    Regex
            //http://localhost:2017/second/Time_2019_6_13 失败 
            //http://localhost:2017/second/Time?year=2019&month=6&day=13  Default
            //http://localhost:2017/test/Time?year=2019&month=6&day=13    Test
            //http://localhost:2017/test/Time_2019_06_13  失败的,只会被一个路由匹配

            //常规路由,一般来说,我们不怎么扩展这个路由
            routes.MapRoute(
                name: "Default",//路由名称,RouteCollection是key-value,key 避免重复
                url: "{controller}/{action}/{id}",//正则规则:两个斜线 3个变量
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                //默认值 没有id变量 就是UrlParameter.Optional  没有action就是index  没有controller是home
            );

        }
    }

下面看下BundleConfig的方法,创建MVC项目时会创建此文件,位于App_Start文件夹下

    public class BundleConfig
    {
        // 有关捆绑的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").
                Include("~/Scripts/jquery-{version}.js"));
                //.Include());

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // 使用要用于开发和学习的 Modernizr 的开发版本。然后,当你做好
            // 生产准备就绪,请使用 https://modernizr.com 上的生成工具仅选择所需的测试。
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_33391499/article/details/103383584