第十二节:MVC中的一些特殊优化

一. 删除WebForm视图引擎

   在MVC框架中检索视图的顺序为:当前控制器下对应的文件夹的aspx文件→share文件夹aspx文件→当前控制器下对应文件夹的cshtml文件→share文件夹的cshtml文件。

   鉴于以上顺序,如果我们使用的MVC5框架中不需要WebForm的视图引擎,可以删除,来提高视图的检索速度。

   同样是在Global.asax中操作,代码如下:

 1  public class MvcApplication : System.Web.HttpApplication
 2     {
 3         /// <summary>
 4         /// 删除WebForm视图即aspx文件视图检索引擎
 5         /// </summary>
 6         protected void RemoveWebFormEngines()
 7         {
 8             var viewEngines = ViewEngines.Engines;
 9             var webFormEngines = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
10             if (webFormEngines != null)
11             {
12                 viewEngines.Remove(webFormEngines);
13             }
14         }
15 
16 
17         /// <summary>
18         /// 网站第一次启动的时候会率先执行,且只执行一次
19         /// </summary>
20         protected void Application_Start()
21         {
22             AreaRegistration.RegisterAllAreas();  //区域
23             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  //过滤器
24             RouteConfig.RegisterRoutes(RouteTable.Routes);   //常规路由
25             BundleConfig.RegisterBundles(BundleTable.Bundles);   //js包、css包、combres
26 
27             //删除WebForm视图即aspx文件视图检索引擎
28             RemoveWebFormEngines();
29             //阻止MVC将版本号发回给浏览器
30             MvcHandler.DisableMvcResponseHeader = true;
31             //注册自定义实例化控制器的容器
32             ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());
33         }
34     }

二. 隐藏MVC版本号

   在Asp.Net MVC框架中,默认打开一个页面,会将MVC的版本显示在浏览器上,如下:

 

  在Global.asax中的Application_Start()方法中添加一句话:MvcHandler.DisableMvcResponseHeader = true;  即可阻止将MVC版本暴露给浏览器,如下:


 

 

 

 

posted @ 2017-12-08 07:59  Yaopengfei  阅读(973)  评论(1编辑  收藏  举报