asp.net mvc 中页面的查询计数
我想计算当我在 asp.net mvc 中请求页面时执行了多少查询。页面逻辑中的困难:求和查询在主控制器操作中执行,但其他查询在小部件控制器操作中执行,这些查询由母版页中的 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果可能,避免使用静态变量。如果所有方法共享相同的控制器实例,则基本控制器上的实例变量将同样容易地工作。我假设您没有实例化新控制器来执行其他操作。
编辑:经过考虑,简单的静态变量将不起作用,因为它将在所有请求之间共享。您需要使用某种字典,该字典由标识请求的唯一 ID 键入。可能基于发起请求时创建的主机地址和时间戳的组合。也许使用应用程序缓存会起作用,这样它就可以从任何地方使用,并在您完成后自动清理。当您考虑多个线程交互的所有可能方式时,这只会变得更加复杂。再说一次,我想说尝试使用实例变量可能是处理这个问题的最佳方法。
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.
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.
扩展您的 MvcApplication 类(在您的 global.asax 中),
然后为 MvcApplication.BeginRequest 事件创建一个处理程序,或者使用您的 BaseController 类扩展您现有的处理程序,
增加计数器
您可以使用hth
extend your MvcApplication Class (in your global.asax) with
then create a handler for the MvcApplication.BeginRequest event or extend your existing handler with
and in your BaseController Class you can increment your counter with
hth
您使用的是 SQL Server 吗?如果是这样,您可以使用“工具”菜单中的 SQL Server Profiler 查看针对特定页面运行的查询数量。
Are you using SQL Server? If so you can use the SQL Server Profiler from the Tools menu to see how many queries are run for a specific page.