I am trying to execute some code once for any page request so I put this code in the Application_PreRequestHandlerExecute method in Global.asax. The problem is that for each page request this methods gets called perhaps 10 times or more. So, the question is how do I get my code to 开发者_Python百科get called only once per request? I tried adding this to the Application_PreRequestExecute method:
const string key = "scanning_forms";
object chack = HttpContext.Current.Items[key];
if (chack == null)
{
// Code I want to execute only once
}
The problem is though that for some reason check is always false! Any ideas? Incidently, I am using Sitecore. I don't think that should be significant though.
Thanks,
Sachin
Dude, seems like this is an overly complicated solution to solve a rather simple problem. Since you've tagged this #sitecore, I assume you're needing this solution in the context of a Sitecore solution.
You should have only 1 layout file (per device) in your solution. It will get executed for each and every page request. Put your code there, in Page_Load() for instance.
And even if you have several layouts, you could make a common base class (Layout, for instance), and have each layout codefile inherit from this. And then do as above.
You can set a session variable and check to see if it exists:
if (Session["RequestMade"] == null) {
//Do logic
Session["RequestMade"] = true;
}
However; i'm not sure that you should be doing this to begin with. Can you please provide some more information as to what you're trying to achieve?
I'm not entirely sure if Kit's solution will work (I would assume requests for resources/pages/etc are seperate).
I believe you will need to verify in your current method whether or not the request is for a page (i.e. check to see if the request is for a *.aspx file, or check to see if the Request is being handled by the PageHandler)
Edit:
I would think a check like this prior to running your code should work (assuming the Handler property has been populated by the time your code is running):
if(HttpContext.Current.Handler is Page)
Look at modifying the HttpRequest pipeline in the Web.Config for Sitecore. A simple solution is to add you own pipeline processor. However, there may be performance considerations for this approach. Another consideration is to use a sublayout or webcontrol in the layout details for the Sitecore items.
That event gets fired for each request. This means that it will fire off for any WebResource.axd resources you may have on your page.
As for ensuring it is only called once, you are doing something like HttpContext.Current.Items.Add(key, something) right?
You probably want to move your code to Application_BeginRequest
, which fires once per request. It's earlier in the lifecycle. See http://www.techrepublic.com/article/working-with-the-aspnet-globalasax-file/5771721.
If your intent is truly once per page (and not with the separate things on that page such as images that generate requests) then you can examine the request headers to determine what's being requested to narrow it down to that page.
One thing I noticed in my own development is that when you run ASP.NET code inside of Visual Studio using the built-in debugger, the global.asax file methods get called even for images and css files and other non-server-side file types. However, when you deploy your code to the server, IIS will not do this (in a typical setup). Are you having this issue in a production environment? Or just on your local machine?
If you determine that the issue is that the method is getting called for all images and other resources, and it continues to be a problem then you could wrap your code in an if block to make sure you're on an ASP.NET page before continuing. Something like this:
// This code only runs if the local file requested has ".aspx" in the name somewhere.
if(HttpContext.Current.Request.LocalPath.ToString().IndexOf(".aspx") > 0)
{
const string key = "scanning_forms";
object chack = HttpContext.Current.Items[key];
if (chack == null)
{
// Code I want to execute only once
}
}
I've typed this from memory so it could be wrong. Also it will lead to false positives if you have a file like C:\INETPUB\WWWROOT\MYSITE\Resources for .aspx files\images\banner.gif
. So you'd probably want to make the check more robust. I'll leave that to you.
精彩评论