查询计数在asp.net mvc的页面 [英] Queries count for page in asp.net mvc

查看:88
本文介绍了查询计数在asp.net mvc的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算,当我在asp.net mvc的请求一个页面有多少查询被执行。 Difficults在页面逻辑:总和查询主控制器动作执行,但其他人 - 在小部件控制器动作,这是由Html.ActionLink在母版页调用。

I want to calculate how much queries are executed when i request a page in asp.net mvc. Difficults in page logic: sum queries are executed in main controller action, but others - in widget controller actions, which are called by Html.ActionLink in master page.

我写了基类中,我是被调用函数来增加查询柜台查询封装所有的控制器 - 静态变量。在页面加载它的工作很好,所有查询都算的,但是当我请求第二页我的静态计数器不复位,我应该怎么办?

I wrote base class for all controllers in which i'm encapsulate query called function to increase queries counter - static variable. On page loading it work nice, all queries are counted, but when i requested second page my static counter doesn't reset, what should i do?

推荐答案

如果使用的是静态变量可能避免的。如果所有的方法共享相同的控制器实例的基础控制器上的一个实例变量将工作一样容易。我认为你没有实例化新的控制器来执行其他操作。

If possible avoid using a static variable. An instance variable on the base controller would work just as easily if all the methods shared the same controller instance. I assume that you're not instantiating new controllers to execute the other actions.

修改:考虑这件事后,一个简单的静态变量将无法正常工作,因为它会在所有请求之间共享。你需要使用某种形式的一些独特的ID标识请求键控字典。或许基于所述HostAddress的组合,并且该请求被启动时创建的时间戳。也许使用应用程序缓存会工作所以它可以从任何地方和自动清理,当你完成。这只会为你考虑所有可能的方式,多线程可能会相互作用,变得更加复杂。同样,我会说试图使用实例变量可能是处理这个问题的最好办法。

EDIT: After thinking about it, a simple static variable won't work as it will be shared across all requests. You'll need to use some sort of dictionary keyed by some unique id identifying the request. Perhaps based on a combination of the HostAddress and a timestamp created when the request is initiated. Perhaps using the application cache would work so it's available from everywhere and automatically cleaned up when you are done. This is only going to get more complicated as you consider all the possible way that multiple threads may interact. Again, I'd say trying to use an instance variable may be the best way to handle this.

这篇关于查询计数在asp.net mvc的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆