首页 新闻 会员 周边

Asp.Net MVC自定义视图引擎问题

0
悬赏园豆:5 [已关闭问题] 关闭于 2015-04-29 18:24

我通过自己定义数组指定了视图的搜索位置,在vs的调试环境中运行起来没有任何问题,但发布release版本放入IIS后就出现了问题,非常诡异,问题如下:

第一次访问:正常请求某个页面

第二次访问:请求到了第一次访问所请求的视图

 

通过一步一步的调试,我发现继承于RazorViewEngine下的自定义视图引擎中的FindView方法执行了两次,第一次其useCache参数值为true,第二次为false,我在方法体内手动修改其为false后程序运行正常。

 

而在FindView方法中,貌似仅仅只是对视图的搜索路径进行了处理,并没有其他特别的代码,现在因为这个useCache可能会导致程序出错,所以有以下几个问题希望知道的朋友能解答一下。

 

1、useCache这个参数是决定了从缓存中读取视图的搜索路径还是视图转换为对象后的缓存?

2、直接将useCache设为false势必会影响程序性能吧,影响到底是否有多大呢?我感觉即使是不作手动处理,执行两次FindView会不会也带来了某些性能问题?

3、如果说useCache参数对性能影响比较大的话,如何手写代码实现其缓存功能呢?

 1 public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
 2         {
 3             string pluginName = "";
 4             bool isPlugin = controllerContext.RouteData.Values.ContainsKey("pluginName") && !string.IsNullOrEmpty(controllerContext.RouteData.GetRequiredString("pluginName"));
 5             //如果请求的路由信息中有pluginName属性,则表示请求的是插件中的控制器
 6             if (isPlugin)
 7             {
 8                 pluginName = controllerContext.RouteData.GetRequiredString("pluginName");
 9 
10                 this.AreaViewLocationFormats = this._areaViewLocationFormats;
11                 this.AreaMasterLocationFormats = this._areaViewLocationFormats;
12                 this.AreaPartialViewLocationFormats = this._areaViewLocationFormats;
13 
14                 this.ViewLocationFormats = this._viewLocationFormats;
15                 this.MasterLocationFormats = this._viewLocationFormats;
16                 this.PartialViewLocationFormats = this._viewLocationFormats;
17 
18                 this.CodeGeneration(pluginName);
19                 this.UpdatePath(pluginName);
20             }
21             else
22             {
23                 this.PartialViewLocationFormats = this._templateViewLocationFormats;
24                 this.ViewLocationFormats = this._templateViewLocationFormats;
25                 this.MasterLocationFormats = this._templateViewLocationFormats;
26             }
28             return base.FindView(controllerContext, viewName, masterName, false);
29         }
问题补充:

这是我的毕业设计中遇到的问题,明天就要答辩了,看来只能通过手动设置useCache为false来掩盖这个问题了,其结果也是带来一点性能问题了,但我还是希望能够解决这个问题,希望知道的朋友能够为我解答一下。

MEZW的主页 MEZW | 初学一级 | 园豆:108
提问于:2015-04-26 23:39
< >
分享
所有回答(1)
0

网上没看到相关解决方法,自己加了缓存,既保证了原有缓存功能,FindView方法也只需要运行一次。

MEZW | 园豆:108 (初学一级) | 2015-04-29 18:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册