简体   繁体   English

如何在.NET中的同一父IIS虚拟目录中解析不同站点的绝对URL?

[英]How do I resolve absolute URLs for different sites in the same parent IIS virtual directory in .NET?

My goal is to share images, css, js, etc. across multiple websites that are all hosted under the same parent virtual directory in IIS. 我的目标是在所有都托管在IIS中同一父虚拟目录下的多个网站之间共享图像,css,js等。 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. 当然,我可以对图像路径进行硬编码,但是我希望.NET能够使用IIS配置智能地解析URL。

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. Parent SiteSubsite1 ,和Subsite2是建立为所有虚拟目录http://parentsite.comhttp://subsite1.comhttp://subsite2.com ,分别。 I want to share images, js, css, etc. across these sites. 我想在这些站点之间共享图像,js,css等。

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 (eg, http://subsite.com/default.aspx )? .NET中是否存在从子站点之一(例如, http: //subsite.com/default)调用时将~/images/logo.jpg解析为http://parentsite.com/images/logo.jpg的方法。 .aspx )?

you should create a cdn subdomain and point the resources files to that eg 您应该创建一个cdn子域,并将资源文件指向该子域,例如

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. ~/images/logo.jpg永远不会解析为应用程序的根目录以外的任何其他内容。 That's the whole point of the ~ . 这就是~

In IIS6 and even IIS7 you can use a new virtual directory, call it "assets", in all of the children sites. 在IIS6甚至IIS7中,您可以在所有子站点中使用一个新的虚拟目录,称为“资产”。

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". 通过这种结构,您可以与所有站点共享相同的“ assets”文件夹,并且仍以“〜/ assets”访问它。 This allows you to use the assets with any of the domains that IIS is answering for. 这使您可以将资产与IIS响应的任何域一起使用。

http://parentsite.com/assets http://parentsite.com/assets

http://childsite.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 ). CDN是一个不错的选择,但通常您会为每个站点(例如http://cdn.parentsite.comhttp://cdn.childsite.com)创建单独的DNS输入,然后创建一些函数来将文件引用为http: //cdn.(currentdomain )。

In my opinion, using an absolute or relative path to the shared assets folder will make life easier for you. 我认为,使用绝对路径或相对路径指向共享资产文件夹将使您的生活更轻松。

One option is create resources.site.com which has the shared images or move it to a CDN. 一种选择是create resources.site.com,它具有共享图像或将其移动到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. 这些站点使用config设置将资源的标准URL格式化。

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. 由于您已经在代码中抽象出resources.site.com,因此它可以位于同一服务器,不同服务器,CDN等中。没关系-您可以灵活地为其提供服务。 You could also start with it on the same server and when you want to optimize for geo, move it to a CDN. 您也可以从同一台服务器上开始,当您要优化地理位置时,将其移至CDN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM