简体   繁体   English

对每个页面请求执行检查(不是每个控制器操作)

[英]Execute check on each page request (not every controller action)

I have an external API check that I want to execute each time a user does requests a page on my site. 我有一个外部API检查,我希望每次用户确实在我的网站上请求页面时都执行该检查。 However I don't want to execute the check every time an action is called - there are quite a lot of partial views, Html.Actions and ajax controller action calls in the views, and checking the API multiple times a page will slow the application down. 但是我不想每次调用一个动作时都执行检查-视图中有很多局部视图,Html.Actions和ajax控制器动作调用,并且多次检查API会使页面运行缓慢下。

I've tried overriding OnActionExecuting in my base controller, but that obviously fires every time an action is called, meaning it will fire on partials, etc. 我曾尝试在基本控制器中重写OnActionExecuting ,但是显然每次调用一个动作时都会触发,这意味着它将在部分引擎上触发,依此类推。

I've also tried implementing Application_ AcquireRequestState in global.asax but this is called multiple times per page, as well. 我也尝试过在global.asax实现Application_ AcquireRequestState ,但这也被称为每页多次。

I was wondering if there was a way to only execute a block of code if there has been a full page request? 我想知道是否有一种方法可以在有整页请求的情况下仅执行代码块?

检查if (!ControllerContext.IsChildAction)

Set a bool saying that it has been executed to the HttpContext.Items collection. 将一个bool声明为已执行到HttpContext.Items集合。 Check for the bool before executing the code. 在执行代码之前检查bool

The HttpContext.Items collection lives for the duration of a single request. HttpContext.Items集合在单个请求期间有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM