ASP.NET MVC 对于视图引擎的优化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET MVC 对于视图引擎的优化相关的知识,希望对你有一定的参考价值。

我经常使用asp.net MVC框架来做网站。总的来说,MVC框架是一个非常优秀的框架。相对于曾经的web form模式,我个人感觉分工更加合理思路也更加清晰,但是交给开发人员的工作也相对变多了。

当使用标准配置的时候在新建了控制器,还没有建视图的时候,运行网站,访问这个我们可以看到

技术分享

因此我们可以判断,默认的视图引擎首先加载的顺序如上图所示

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

难道说每次都需要搜索这么多次才加载到我需要的cshtml文件,总感觉这样并不是很好,虽说对于性能的影响是比较小的,而且,还可以通过缓存技术,解决经常查看的网页速度的问题。不过终究是个问题还是学会如何解决的好。

默认的mvc视图引擎加载webform的视图引擎和Razor视图引擎,

我们只需要在服务器开始的时候也就是Application_Start事件发生的时候,将原有的引擎清空,加入Razor视图引擎就可以了。

在global.asax.cs文件中

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            ViewEngines.Engines.Clear();//清除原有引擎
            ViewEngines.Engines.Add(new RazorViewEngine());//加入Razor引擎
            

        }

 

加入Razor引擎以后

技术分享

其实我觉得这样就可以了,直接在对应文件夹中首先搜索cshtml

不过,总要研究下他的引擎,不过我只关注他视图查询读取,通过使用.NET Reflector 进行解析发现,Razor视图引擎的代码原来是这样的

public RazorViewEngine(IViewPageActivator viewPageActivator) : base(viewPageActivator)
        {
            base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
            base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
            base.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
            base.FileExtensions = new string[] { "cshtml", "vbhtml" };
        }

于是我们可以继承RazorViewEngine来自定义引擎搜索的位置和顺序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ALLTest.App_Start
{
    public class MyViewEngines: RazorViewEngine
    {
        public MyViewEngines()
        {

            base.AreaViewLocationFormats = new string[]

           {

                "~/Areas/{2}/Views/{1}/{0}.cshtml",

                "~/Areas/{2}/Views/Shared/{0}.cshtml",

           };

            base.AreaMasterLocationFormats = new string[]

            {

                "~/Areas/{2}/Views/{1}/{0}.cshtml",

                "~/Areas/{2}/Views/Shared/{0}.cshtml",



            };

            base.AreaPartialViewLocationFormats = new string[]

            {

                "~/Areas/{2}/Views/{1}/{0}.cshtml",

                "~/Areas/{2}/Views/Shared/{0}.cshtml",

            };

            base.ViewLocationFormats = new string[]

            {

                "~/Views/{1}/{0}.cshtml",

                "~/Views/Shared/{0}.cshtml",

            };

            base.MasterLocationFormats = new string[]

            {

                "~/Views/{1}/{0}.cshtml",

                "~/Views/Shared/{0}.cshtml",

            };

            base.PartialViewLocationFormats = new string[]

            {

                "~/Views/Shared/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",

            };

            base.FileExtensions = new string[]

            {

                "cshtml",

            };

        }


    }
}

然后在服务器启动时,使用这个自己的引擎估计能起到更好的效果

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //
            ViewEngines.Engines.Clear();
            
            ViewEngines.Engines.Add(new MyViewEngines());

        }

以上是关于ASP.NET MVC 对于视图引擎的优化的主要内容,如果未能解决你的问题,请参考以下文章

ASP.Net MVC开发基础学习笔记:三Razor视图引擎控制器与路由机制学习

Asp.net MVC 移除视图引擎(WebFormViewEngine或者RazorViewEngine)

ASP.NET MVC 视图引擎比较

ASP.NET MVC 最好的视图引擎是啥?

ASP.NET MVC 4 视图

ASP.NET MVC 最好的视图引擎是什么?