Search code examples
asp.nethttphandlerihttphandler

IHttpHandler ProcessRequest not being called


I am using an IHttpHandler in my WebForms application to dynamically load .resx resources into javascript at execution time with the appropriate localization. I have setup a demo project here for reference: https://www.dropbox.com/s/qoo9bm7qtzgvhbj/TestJs.zip

I've modified the source from here (slightly): https://github.com/RickStrahl/Westwind.Globalization to get it working.

The concept is simple enough. When I initially hit a page, such as Default.aspx, ProcessRequest is called for both <script /> tags that are loaded into the file by calling:

JavaScriptResourceHandler.RegisterJavaScriptGlobalResources (this, "GlobalResources", "App_GlobalResources\\Global");
JavaScriptResourceHandler.RegisterJavaScriptLocalResources (this, "LocalResources");

These two calls add these tags to the <head /> section of the page:

<script src="/JavaScriptResourceHandler.axd?ResourceSet=App_GlobalResources\Global&amp;LocaleId=en-US&amp;VarName=GlobalResources&amp;ResourceType=resx&amp;ResourceMode=1" type="text/javascript"></script>
<script src="/JavaScriptResourceHandler.axd?ResourceSet=Default.aspx&amp;LocaleId=en-US&amp;VarName=LocalResources&amp;ResourceType=resx&amp;ResourceMode=0" type="text/javascript"></script>

A simple HttpHandler invocation with query parameters. This works for the first page load, but subsequent calls to other pages with the same scripts do not call ProcessRequest. If I manually follow the correct httpHandler address in my browser, a valid script is loaded, but it is the same script that was loaded for the previous page, even though the query string is different.

I am convinced that it must be some weird caching mechanism, but I am not sure what. My web.config is basic enough:

<system.webServer>
    <handlers>
        <add name="JavaScriptResourceHandler" verb="GET" path="JavascriptResourceHandler.axd"
            type="Westwind.Globalization.JavaScriptResourceHandler,Westwind.Globalization" />
    </handlers>
</system.webServer>

I've also tried setting IsReusable to false but with no effect. Does anyone else with more experience with HttpHandlers have some thoughts?


Solution

  • Turns out I was right, and was simply overlooking the caching taking place inside of ProcessRequest:

    HttpCachePolicy cache = Response.Cache;
    
    cache.VaryByParams["LocaleId"] = true;
    cache.VaryByParams["ResoureType"] = true;
    cache.VaryByParams["IncludeControls"] = true;
    cache.VaryByParams["VarName"] = true;
    cache.VaryByParams["ResourceMode"] = true;
    

    The main thing that was changing for me was the ResourceSet parameter, so simply adding cache.VaryByParams["ResourceSet"] = true; did the trick.