2

My goal is to share images, css, js, etc. across multiple websites that are all hosted under the same parent virtual directory in IIS. Of course, I could hard code the images paths, but I'd hoped .NET had a method that could intelligently resolve URLs using the IIS configuration.

Here is my file/virtual directory structure:

Parent Site (Virtual Directory)
  default.aspx
  mypage.aspx
  otherpage.aspx
  images
    - logo.jpg
  css
    - site.css
  Subsite1 (Virtual Directory)
    - default.aspx
    - products.aspx
  Subsite2 (Virtual Directory)
    - default.aspx
    - products.aspx
    - miscellaneous.aspx

Parent Site, Subsite1, and Subsite2 are all virtual directories set up for http://parentsite.com, http://subsite1.com, and http://subsite2.com, respectively. I want to share images, js, css, etc. across these sites.

Is there a method in .NET that will resolve ~/images/logo.jpg as http://parentsite.com/images/logo.jpg when called from one of the subsites (e.g., http://subsite.com/default.aspx)?

Devin Burke
  • 13,642
  • 12
  • 55
  • 82
  • 2
    How is .NET going to know that "subsite.com" and "parentsite.com" are in any way related? The only thing it can relate to is the current URI request. Perhaps just creating virtual directories on the children that link to a shared folder in the parent so they can all references /assets/ ? – Nick Bork May 07 '12 at 20:09
  • 1
    Create a folder in the root, call it assets. Place all of your commonly shared assets there. Inside of each "virtual directory" create a new Virtual Directory for assets which references the roots assets folder. Then ~/assets will always end up being the root site's assets folder. parentsite.com/assets , childsite.com/assets are the same thing – Nick Bork May 07 '12 at 20:25
  • IMHO, while this will work a CDN type setup has more benefits. 1) caching! 2) [more connections at the same time](http://www.browserscope.org/?category=network). – Eonasdan May 07 '12 at 20:41
  • @Eonasdan while a CDN may work it would require that Justin either be ok with sharing the same domain (cdn.parentsite.com) on all children sites or setting up seperate CDN sites for each domain (cdn.everysite.com). Both solutions will work it just comes down to what works best for what he needs. – Nick Bork May 07 '12 at 20:58
  • @NickBork yes, true. I guess I was already assuming that from the wording of his question. Please take your comments and turn them to answer so that he can select it and I can upvote it :) (or I can add it to mine to make it more complete) – Eonasdan May 07 '12 at 23:08

3 Answers3

3

you should create a cdn subdomain and point the resources files to that e.g.

http://cdn.parentsite.com/images/logo.jpg
http://cdn.parentsite.com/css/site.css

etc..

This~/images/logo.jpg will never resolve to anything other then the root of the app. That's the whole point of the ~.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
3

In IIS6 and even IIS7 you can use a new virtual directory, call it "assets", in all of the children sites.

Updated site layout:

Parent Site (Virtual Directory)
     default.aspx
     mypage.aspx
     otherpage.aspx
     assets
          -images
               - logo.jpg
          -css
               - site.css    

Subsite1 (Virtual Directory)
     - default.aspx
     - products.aspx
     assets (Virtual Directory to parent assets)

Subsite2 (Virtual Directory)
     - default.aspx
     - products.aspx
     - miscellaneous.aspx 
     assets (Virtual Directory to parent assets)

With this structure you can share the same "assets" folder with all sites and still access it as "~/assets". This allows you to use the assets with any of the domains that IIS is answering for.

http://parentsite.com/assets

http://childsite.com/assets

A CDN is a good option but often times you would create seperate DNS enteries for each site like http://cdn.parentsite.com and http://cdn.childsite.com and then create some function to reference the files as http://cdn.(currentdomain).

In my opinion, using an absolute or relative path to the shared assets folder will make life easier for you.

Nick Bork
  • 4,831
  • 1
  • 24
  • 25
1

One option is create resources.site.com which has the shared images or move it to a CDN.

Then, in each of the sites that consumes those resources, there is a configuration settings where the resources are. Those sites, format fully qualified urls to the resources using the config setting.

Since you've abstracted out resources.site.com in your code, it could be on the same server, different server, in a CDN etc... It doesn't matter - you're flexible how you serve them up. You could also start with it on the same server and when you want to optimize for geo, move it to a CDN.

bryanmac
  • 38,941
  • 11
  • 91
  • 99