不同域上的不同网站,一个 .NET MVC 应用程序?
是否可以拥有一个 .NET MVC 应用程序,并可以从不同的域访问它,从而使内容依赖于域?
例如,www(dot)site1(dot)com 和 www(dot)site2(dot)com 都将指向我的服务器的 IP,并指向 IIS 中的同一个网站。 我的 .NET MVC 应用程序将驻留在该网站中。 现在,我希望能够知道哪个站点(域名)触发了 ControllerAction,并采取相应的行动(例如,在 Index 操作中显示主页的不同内容,或者允许/阻止访问分配给特定站点的特定内容) 。
我将不胜感激任何帮助。 我可以接受传递给所有控制器操作的额外参数(可能使用路由),但如果有更优雅的解决方案那就更理想了。
Is it possible to have one .NET MVC application, and have it accessible from different domains, in such a way that the content will be domain-dependant?
For example, both www(dot)site1(dot)com and www(dot)site2(dot)com will point to my server's IP, and to the same website in IIS. In that website my .NET MVC application will reside. Now, I want the ability to know which site (domain name) triggered the ControllerAction, and act accordingly (for example, display different content for the homepage in the Index action, or allow/prevent access to specific content assigned to a specific site).
I would appreciate any help on this. I can accept an extra parameter passed to all controller actions (probably using Routing), but if there's a more elegant solution that would be ideal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那么,您始终可以从 Request.RawUrl 属性获取域。
正如 Mercer 提到的,将它们部署为两个独立的 Web 应用程序将是更好的解决方案。 如果这是不可能的,我会尝试设计一些相对通用的东西来检查域并为每个域返回不同的视图。
Well, you can always get the domain from the Request.RawUrl property.
As Mercer mentioned, deploying these as two separate web apps would be a better solution though. If that isn't possible, I would try to design something relatively generic that would check the domain and return different Views for each domain.
我写了一篇博客文章有关如何通过下载示例 Web 应用程序执行此操作。
它使用一个抽象的基本控制器,该控制器知道它所调用的站点 - 通过创建从该基类继承的控制器,您可以自动访问当前请求的当前“站点”。
它还允许您从单个数据库加载所有站点 - 如果您使用共享主机,或者如果您运行自己的服务器,则无需为其设置新数据库,可以节省一点托管费用您创建的每个网站。
I have written a blog post about how to do this with an example web application for download.
It uses an abstract base Controller that is aware of the site it is being called for - by creating controllers that inherit from this base class you have automatic access to the current "site" for the current request.
It also allows you to load all your sites from a single database - can save you a bit on hosting fees if you're on a shared host, or if you run your own server you don't have to set up a new database for each site you create.
您可以通过以下方式轻松访问请求中使用的域名:
您可以在有权访问 Request 对象的任何地方执行此操作。
You can easily access the domain name used in the request with something along the lines of the following:
You can do this in anywhere you have access to the Request object.
一个优雅的解决方案是为 2 个域进行 2 次部署,并分离内容。
您仍然可以拥有通用内容,但在应用程序内部不进行硬编码的情况下分离内容是一个胜利的局面。
An elegant solution would be to have 2 deployments for 2 domains, and to separate content.
You could still have common content, but separating the content without hardcoding this inside the application is a win situation.
如果您使用不同的数据库来保持数据独立,则在“会话开始”中将应用程序配置为基于“服务器名称”变量使用其中一个数据库。 然后将工作连接字符串放入用户的会话中。
然后在应用程序的其余部分使用此连接字符串。
If you use different databases to keep the data separate, then in the Session Start configure the application to use one of the databases based on the Server Name variable. Then place the working connection string in the session for the user.
Then use this connection string in the rest of the application.