MVC视图引擎优化
请首先看如下内容:
未找到视图"Index"或其母版视图,或没有视图引擎支持搜索的位置。搜索了以下位置:
~/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
版权归二师弟和博客园共同所有,欢迎转载和分享知识。转载请注明出处!
Mvc4和mvc3是这样查找的,其中*.aspx和*.ascx以及*.vbhtml都是我们不需要的。
我们可以想象,这里不管采用什么办法,肯定至少4次访问文件系统并判断对应文件是否存在,才会找到我们的index.cshtml,这样的开销小型应用可以忽略,如果并发量千万级(总之很大,具体未测)肯定对性能有一定影响。
怎么避免,不要去查找*.aspx,*.ascx以及*.vbhtml?
经过几番折腾,我们成功找到了解决办法:
Mvc中有两个视图引擎,webformengine和razorengine,解决办法就是,确定不使用webform的情况下,移除webformengine,然后移除razorengine并添加改写了的razorengine。
移除的代码在global.ascx.cs的application_start方法中这样写:
ViewEngines.Engines.Clear();
重写razorengine的方法:
public class Engine : RazorViewEngine
{
public Engine( )
{
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/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
};
base.FileExtensions = new string[]
{
"cshtml",
};
}
}
然后添加改写的视图引擎:
ViewEngines.Engines.Add(new Engine());
版权归二师弟和博客园共同所有,欢迎转载和分享知识。转载请注明出处!
最终application_start方法如下:
最新文章
- Linux 系统命令
- MemCache
- Python学习【第四篇】用户输入及判断
- mac(linux) 上如何安装ant
- centos6.6编译安装lnmp系列之nginx
- ADO.NET数据库操作助手类
- HTML JavaScript简介
- 使用maven配置基本Mybatis
- PHP MySQL 插入多条数据
- hibernate 数据关联多对多 4.1
- Mac OS X在建筑Python科学计算环境
- hdu 4107当卡段树
- didMoveToSuperView 引发的思考
- 使用Java实现二叉树的添加,删除,获取以及遍历
- python中logger模块的应用
- vue 用huilder打包APP时,安卓按返回键就退出App改为按两次再退出App
- centos 6.4配置samba+ldap认证
- Linux 远程登录配置
- Android-Java-类与对象的关系
- SQLServer2005 CASE WHEN在项目中实例-查询显示值替换
热门文章
- ORA-00020: No more process state objects available故障一例
- Hacker communities collection
- 网格弹簧质点系统模拟(Spring-Mass System by Verlet Integration)附源码
- 以最简单的登录为例,诠释JS面向对象的简单实例
- [No00000B]MS OFFICE 2013 快捷键大全
- Func<;T,TResult>;泛型委托
- ajax设置自定义请求头信息
- CSS3之文本阴影text-shadow
- js模拟高级语言的重载
- usb驱动开发18之设备生命线