ASP.NET: change link to static asset dynamically - asp.net

We are building a internal static asset server. Each environment (dev, staging, prod) has its own asset server, and the asset is reference throughout the web application (html, aspx, ascx, css, javascript, etc...)
To reference the correct asset server in the correct environment, one solution is to write a http module to intercept response before it gets to the client and change the URL according. I am just thinking that this might not be the most scalable solution since this http module is going to get executed for every request and basically parse the whole response (some are huge) before the client gets it.
I am also thinking to use a client side javascript to change the reference on client side, but this might not work as nicely has a http module.
Any thoughts? What's the industries best practice in ASP.NET?

I might create my own LinkToStaticAsset control. It would only accept the path relative to the static asset server of each asset. I would have it generate the full URL by including the base path from configuration.

If you want your site to function (even if only partially) without a Javascript dependency then you should keep this server-side.
An alternative approach to what you have mentioned above is to do this at the application level, i.e. have a library method which generates your static asset URL's and is configured to point at particular server(s) via your web.config.
It would go something like this:-
(In your App_Code folder or a referenced assembly)
public static class Util
{
public static string AssetUrl(string relativePath)
{
// returns asset server address from web config with relative path appended
}
}
(In web.config)
<appSettings>
<add key="AssetServerBaseUrl" value="http://foo.bar" />
...
</appSettings>
(In your aspx file)
<img src='<%= Util.AssetUrl("img/myimage.jpg") %>' ... />

Related

Making a route exception for a static file

I've got some software out in the wild that needs to download some static file from a specific place on my domain: domain.net/somefolder/setup.exe. While I plan to change this, it's going to take a long time due to constraints out of my control.
In the meantime, how can I deal with this in my Angular2 router? Essentially, if Angular sees this specific url it should not try to resolve the url and instead serve the static file similar to how it would behave if you accessed static files from the assets folder.
{ path: 'somefolder/setup.exe', ... }
I ended up just doing a url rewrite on the web server level within a config.

Meteor : Access to my public/lib from server

According to the Meteor Doc, the public fodler is accessible for both server and client. But, if I just need to use the HTML head elements to access it, how can I do it from server ?
I made a lib with very specific functions, and I'm tired of copy pasting them at the top of each of my server .js file.
Could anyone give me the tip ? I couldn't find out on google :/
Thanks you,
David
You are making your life harder than necessary. Any folder that is not called client, server, private, or public is shared by the client and the server. Just put your shared .js files into any folder, say, /common, and they will be available (loaded) on both the client and the server.
The public folder is specifically for assets that are not loaded automatically by the server, but are instead served statically over HTTP, similar to the static functionality of express. It's the place for images and other assets you want on the client.

Why should I declare <mvc:resources> in my configuration file

I'm having hard time understanding the purpose of <mvc:resources mapping="..." location=".."/>section in my configuration file.
my project structure...
--WebContent
-----META-INF
-----static
-----------images
------------js
------------css
-----WEB-INF
--------jsps
--------spring-configs
--------web.xml
I'm able to access files under images, js and css folders even when I don't specify this section in the spring servlet config file. So why do I need to specify this? What purpose does it serve exactly?
For the record, this is what I have in my config file.
<mvc:resources mapping="/static/**" location="/static/" />
You don't need it if that's where you put your resources. Everything at the root of your web application (WebContent), which isn't WEB-INF or META-INF is publicly available and the Servlet container can serve it to any client.
The <mvc:resources ... /> element is meant to serve resources which are within WEB-INF which is not available directly to clients. They must be served by your application.
Adding to what #Sotirios has answered, <mvc:resources/> can resolve static resources from Classpath resources ( for e.g., a jar file). This opens up the possibility of packaging your static resources in a self-contained jar module along with your business logic (although very few people use this approach in command based framework like Spring - this is more prevalent in Component based frameworks like JSF).
Apart from this there are other benefits of using this tag as mentioned here. I'm quoting
The cache-period property may be used to set far future expiration
headers (1 year is the recommendation of optimization tools such as
Page Speed and YSlow) so that they will be more efficiently utilized
by the client. The handler also properly evaluates the Last-Modified
header (if present) so that a 304 status code will be returned as
appropriate, avoiding unnecessary overhead for resources that are
already cached by the client

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

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

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.

Resources