如何设置跨多个域名共享代码 (ASP.NET)?
我已经建立了一个网站,现在客户希望将其拆分为三个不同的域。最好的方法是什么? 这是我到目前为止所拥有的。
- c:/website1/ 指向 www.website1.com
- c:/website1/vd1/ 指向 www.website2.com
- c:/website1/vd2/ 指向 www.website3.com
我正在使用的网络主机已完成此操作以下方式,但现在我收到一堆错误,似乎没有看到 App_code 文件夹。我需要做很多改变吗?这对位置参考有何影响?
I have built a website and now the customer wants to split it between three different domains. What is the best way to do this?
This is what I have so far.
- c:/website1/ points to www.website1.com
- c:/website1/vd1/ points to www.website2.com
- c:/website1/vd2/ points to www.website3.com
The webhost I'm working with has done it the following way, but now I'm getting a bunch of errors that seems like it's not seeing the App_code folder. Do I need to make a lot of changes? How does this affect the location references?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到的问题是,通过按照您所做的方式配置站点,www.website2.com 和 www.website3.com 是单独的应用程序,并且无法看到 /vd1/ 或 /vd2/ 之外存在的任何内容或代码分别(所以是的,这将是 c:\website1\ 中的 /app_code/ 文件夹,以及其中可能存在的任何图像、CSS、用户控件、母版页等)。
如果您共享的只是 /app_code/ 文件夹中的一些逻辑,您是否可以将其复制到 Visual Studio 中的新项目中?理想情况下,您需要创建一个新的类库项目,并在其中包含 .cs 文件。将其编译到 .dll 文件中,然后在您的网站项目中添加对该库的引用。
然而,这对于用户控件或您可能想要使用的其他常见页面(例如登录、注册等)来说效果不佳。
然后确保在每个虚拟目录中创建一个 /bin/ 文件夹并将库复制到其中。您可能还需要要求您的虚拟主机将它们配置为 www.website1.com 中的应用程序。
编辑以回复评论
那么,是的,您可能确实有大量工作要做,但不知道为什么要拆分网站的各个部分像这样到不同的域上,我无法真正提供如何做到这一点的解决方案 - 如 Bryan 指出,您可以将多个域(称为主机标头)映射到 IIS 中的单个站点,所以你可以让所有三个都指向同一个根。
如果您希望有不同的外观和感觉,您需要考虑有一个核心“Base Page”类型类,该类继承自 System.Web.UI.Page 并让您的页面继承自该类,然后,您可以根据 URL 中的 ServerName 交换模板 - 例如,如果您使用 MasterPages,则需要在 OnPreInit 方法中执行此操作。
您还可以在其中添加其他逻辑,以确保将用户发送到网站的正确部分(如果需要)。
The problem you have is that by configuring your sites as you have done, www.website2.com and www.website3.com are separate applications, and cannot see any of the content or code that exists outside of /vd1/ or /vd2/ respectively (so yes, that would be the /app_code/ folder in c:\website1\, along with any images, css, user controls, master pages, etc that might be in there).
If all you are sharing is some logic in the /app_code/ folder, are you able to copy that to a new project in Visual Studio? Ideally you'd want to create a new Class Library project, and just have the .cs files in there. Compile that into it's .dll file, and in your web site project, Add a Reference to that library.
However this doesn't work as well for user controls, or other common pages you might want to be using (such as login, registration, etc).
Then ensure that you create a /bin/ folder in each of the virtual directories and copy the library into there as well. You may need to ask your webhost to configure them as Applications in the www.website1.com as well.
Edit to respond to comment
Then yes, you probably do have quite a bit of work ahead of you, but without knowing the reason why you're splitting out parts of the site like this onto different domains, I can't really offer a solution as to how to do that - as Bryan points out below, you can map multiple domains (known as host-headers) to a single site in IIS, so you could have all three pointing to the same root.
If you're then looking to have a different look and feel, you'd want to look at having a core "Base Page" type class, that inherits from System.Web.UI.Page and have your pages inherit from that instead, you can then swap out the templates based on the ServerName in the URL - if you're using MasterPages for example you would need to do this in the OnPreInit method.
You would also be able to add additional logic in there to ensure that the user is sent to the correct section of the site if that's a requirement.
仅仅因为您有多个域并不意味着您需要单独的应用程序。这些虚拟目录有什么意义呢? IIS 很乐意为同一个应用程序提供多个域服务。
Just because you have multiple domains doesn't mean you need separate applications. What is the point of these virtual directories? IIS will happily serve multiple domains to the same app.