views:

74

answers:

1

I'm wondering if there's an easy way to specify a CDN for all content that I reference through Url.Content in my views.

Something that I could configure in my Web.config file in a way similar to the following.

    <cdn>
        <add filetype="css" root="http://mycdn.mydomain.com/stylesheets/" />
        <add filetype="js" root="http://myjscdn.mydomain.com/js/ />
    </cdn>

Then, I could just have <%= Url.Content("~/Content/StyleSheets/What.css") %> and it would output http://mycdn.mydomain.com/stylesheets/Content/StyleSheets/What.css.

If nothing is available, I'll just do it myself via extension methods, but I was wondering if it was possible out of the box.

+1  A: 

I don't believe there is anything built in to facilitate this for you. That said, is adding this to Web.config really necessary?

I guess if you don't want to include your CDN-distributed items on every page in your application, I could understand extrapolating the URLs to a central location somewhere, but even then it seems like you could just write some extension methods that return a constant string instead of messing with the Web.config. I know some people cringe at this because you have to recompile to change the URL's, but how often are they going to change?

In the event, however, that you do want to include these CDN-distributed items on every page, what's wrong with throwing the URLs in your master page? In most cases, changing your master page is just as simple as changing a web.config value.

Scott Anderson
I just like to have my configuration in my web.config file. It's easier to spot and easier to read than code. And it's not just for files that are included in my master pages, I have loads of smaller javascript files that I include in multiple pages and I don't want to forget one of them.
Sefyroth
It makes sense to configure some things, I only meant for you to think about it. I've come into projects too often that have literally 150 "toggle" flags and various values in the web.config, and stuff actually ends up getting duplicated because people forget what they've done in there :)
Scott Anderson
I totally understand that. Happens here too. I just think it's practical, as if I decide to add a CDN for swf only later on, I will just have to add the <add /> element with the proper values and I'm set. Otherwise, I'd have to change all my calls to methods and so on. Thanks for the input.
Sefyroth