Not able to access session in Httpmodule for static resources like css /js/ image files - asp.net

I have created a HTTPModule. Session information is retrieved using PreRequestHandlerExecute. It is working well for aspx pages. But while loading css/js or any image files, Session is null.
I have gone through the link ASP.NET CSS file not loaded when adding HttpModule to web.config
But could not get the implementation.
Anyone has any idea about this?

I think you'll find that your static files are being handled by the StaticFileHandler and as such wouldn't fire off session events as the session won't be being activated for efficiency purposes.
You could configure IIS to pass static files through a different handler (the PageHandler for example) but really you'd need to know if that was required first. You haven't described why you want sessions to be available for static files.

Related

Serving extensionless static files in ASP.NET MVC; getting 404s

For whatever reason (ugh, just assume we have to), we have a JavaScript file and a CSS file without an extension in our ASP.NET MVC application. The JavaScript file is at path ~/Scripts/js and the CSS file is at ~/Styles/css. These are static files containing JS and CSS respectively, but without file extensions.
Right now, when I try to load the resources in a browser, I get a 404 for those two paths. What do I need to do to make my ASP.NET MVC application serve these extensionless files (and serve them with the correct MIME types)? Something in the web.config and mapping a particular URL pattern to the HTTP handler for static files, I'm guessing. Apparently my Googling skills are inadequate—forgive me.
I think esmoore68 probably has the best answer... but... if you don't want to do that, I wonder if you could declare the style (and the script) on the page (rather than reference the files), and maybe use labels on the pages where you need these, and actually open the files in code-behind, read them as text, then write them into the labels on the page load?
I haven't tried anything like that, specifically, but it seems like it would work.
If it does, maybe you could create a user control so you can just put that in all the pages, rather than repeating it every time.

Where to place a frequently accessed file in an ASP.NET application?

I am writing an ASP.NET Web Form application.
Each time a request arrives my aspx page, I will use an object to do some logic. This object is deserialized from a pre-made file.
There'll be many requests to my aspx page so I don't want to deserialize this object every time a request comes in. Becuase as I remember, ASP.NET will serve different requests with different threads. And the one thread reading the file will block other threads from reading this file. So hurt the performance. (But I am not sure about this, please correct me if I am wrong.)
The file will be updated weekly or bi-weekly. So I need to re-deserialize the corresponding object once the file is updated.
My questions are:
Where should I put the code to deserialize the object from file?
How to monitor the file change?
My current plan is:
Deserialize the object from file at the Application_Start() event. And store this object in Cache.
Use FileSystemWatcher to monitor the folder containing the file and update the object once file is modified.
Is this design OK? Is there any more reasonable design?
Thanks.
Store the file in the App_Data.
FileSystemWatcher is ok but I would suggest it to run as a windows service. You could have a page dedicated to refresh the cache (ex: www.webpage.com/refreshcache.aspx). You could also just setup a scheduler to refresh the cache every night.

Retrieve embedded static resources (css/img/etc) using Virtual Path Provider instead of GetWebResourceUrl

I have an asp.net 4.0 webforms website with a CSS stylesheet which I've embedded into a separate assembly.
If I load the css using ClientScript.GetWebResourceUrl it loads, but does it with that /WebResource.axd?whateverlongstringofstuff
I was hoping that I could use a Virtual Path provider so that i can simply reference it by linking to say ~/Custom/my.css
where ~/Custom/* would go through the VPP and serve it from the embedded resource.
the reason I was doing this is I want users to be able to easily override my css by adding the actual css file in that location so it serves that one instead.
However, no matter what I try, I get a 404 on that resource url, even though in my custom ResourceResolver, the fileExists resolves to true (because it does find it in the resources assembly).
So I suspect this is because IIS is configured to serve static files (css, jpg, png, etc) directly, bypassing anything I could add to the pipeline, and the only way around this would be to have my users change their web.config or IIS settings.
Is this true? because if it is I'll abandon this and go back to the WebResource.axd, but it would really be nice if this could be done

Cannot route static files in ASP.NET WebForms

We have legacy code to maintain and, to solve a specific customer customization problem, we want to route calls to some files to other files. That is, when the app calls a particular ASPX, it will end up hitting another ASPX.
If you call:
www.foo.com/admin/admin.aspx
It will actually hit:
www.foo.com/customizations/customer1/admin/admin.aspx
This is not a good design but this is legacy code. We just want to solve this.
We are using the System.Web.Routing framework to solve it. This works fine when you set RouteExistingFiles to true, except for static files (CSS, JavaScript and Images).
When I first tried it, it retrieved this error:
There is no build provider register for the extension '.css'.
So I did register a build provider in the web.config file for the .css extension. I used this build provider: PageBuilderProvider because someone recommended it in the internet.
It works! But the CSS is being served with text\html content type.
How do I achieve this?
TL;DR: I want to use routes in ASP.NET Web Forms to make a call for a specific CSS file to actually retrieve another one. A customer needs this for customization.
Try coding a HttpHandler. I had to do something similar but for PDF files, I coded a custom HttpHandler in the end - works very well. You can even set the content type in the HttpHandler code and have a pattern matched path the handler will be used for in the web.config. You can also configure it in web.config not to execute if the path does not point to an existing file e.g. so a 404 is returned without having to code that in the handler itself. I can't post my code (VB.NET) ATM because I'm using a tablet but google search for tutorials. You will also probably need to use the TransmitFile function to actually write out the css file. Is it a web forms project or web site? If its a web site there is a special way of registering the HttpHandler in the web.config.

Programmatically deciding what file a URL should point to with ASP.NET 3.5 and IIS 7

Is it possible to programmatically resolve a URL to a file using ASP.NET and IIS? Specifically I'd like the file to be outside of my Virtual Directory (could be anywhere on the local file system). So if a URL comes in like http://mysite/somepicture.jpg I'd like to be able to return c:\mypicture.jpg. I looked into creating an IHttpModule for URL rewriting but that isn't quite what I need - it's limited to URLs within the existing site.
You cannot achieve it by URL rewriting as the file is not hosted on your Web site. You should use Response.WriteFile method in an HttpModule or HttpHandler to manually stream the file to the user.
I would like to add to Mehrdad's response by saying that you need to make sure your app has rights to the folder the files you want live in. That way you can dish it out as Mehrdad suggested.

Resources