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

在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;
        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值