ASP.Net MVC刪除多余视图引擎

--------------------- 
作者:巔峰白羊 
来源:CSDN 
原文:https://blog.csdn.net/qq_33961062/article/details/80449791 

--------------------- 

在ASP.NET MVC中,访问网页是通过路由机制,路由通常是先访问控制器中的Action方法在通过Action访问相应的View中的代码,这其中需要找到相应名称的网页,搜索的时候不仅会搜索本来需要的网页也会搜索原本不需要的网页,比如说本来想访问

Index.cshtml 但是路由是Home/Index 框架会帮你搜索Views文件夹中Home子文件夹中的Index.cshtml或Index.vbhtml或者

Index.aspx,多多少少会影响到网站响应的速度,所以建议在Global.asax即网站开始Run的时候就将其他的不需要的搜索动作删除。

遵守配置原则现在App_Start文件夹下加一个类ViewEngineConfig.cs

 public class ViewEngineConfig
    {
        public static void Register(ViewEngineCollection viewEngines)
        {
            viewEngines.Clear();
            viewEngines.Add(new CSharpRazorViewEngine());
        }
        internal class CSharpRazorViewEngine : RazorViewEngine
        {
            public CSharpRazorViewEngine()
            {
                AreaViewLocationFormats = new[]
                {
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                    "~/Areas/{2}/Views/Shared/{0}.cshtml"
                };
                AreaMasterLocationFormats = new[]
                {
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                    "~/Areas/{2}/Views/Shared/{0}.cshtml"
                };
                AreaPartialViewLocationFormats = new[]
                {
                    "~/Areas/{2}/Views/{1}/{0}.cshtml",
                    "~/Areas/{2}/Views/Shared/{0}.cshtml"
                };
                ViewLocationFormats = new[]
                {
                    "~/Views/{1}/{0}.cshtml",
                    "~/Views/Shared/{0}.cshtml"
                };
                PartialViewLocationFormats = new[]
                {
                     "~/Views/{1}/{0}.cshtml",
                    "~/Views/Shared/{0}.cshtml"
                };
                MasterLocationFormats = new[]
                {
                     "~/Views/{1}/{0}.cshtml",
                    "~/Views/Shared/{0}.cshtml"
                };
                FileExtensions = new[]
                {
                    "cshtml"
                };
            }
        }
    }
然后在Global.asax中
protected void Application_Start()
        {
            ViewEngineConfig.Register(ViewEngines.Engines);//重新註冊引擎,只用razor視圖的cshtml
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            Application["OnLineUserCount"] = 0;
        }


 

猜你喜欢

转载自blog.csdn.net/hiose89/article/details/88946265