码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-02-08 13:31:16      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

我经常使用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 对于视图引擎的优化

标签:

原文地址:http://www.cnblogs.com/zhaofangzhou/p/5184922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!