views:

152

answers:

2

Hi all,

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?

+1  A: 

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") %>' ... />
AdamRalph
@AdamRalph: Do you have any example for the alternative approach you mentioned? The only thing I can think of is to use a build provider to change all link to static asset per build.
Herman
I've some added sample code (written free hand)
AdamRalph
@AdamRalph: I think that'll only work for dynamic pages like aspx/ascx. What about background image link in CSS or general anchor tag in HTML?
Herman
That's correct, it will only work for dynamic pages. The question was asked in the context of ASP.NET so I thought this solution might suit you. If you also have static content then you'll have to go with another solution.
AdamRalph
+1  A: 

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.

John Saunders
@John Sannders: How's that going to work with css, js and html files?
Herman
<LinkToAsset type={Link,Script,Iframe,Style,...} RelativeUrl="path"/>
John Saunders