问题
i have created a new web forms application in visual studio 2010 with just two pages Default.aspx
and About.aspx
. what i want is that when i debug the app in visual studio development server it and enter http://company1.localhost:1023
it should just display Default page with message This is default page for company1
. How can i do it with just playing with url routing as opposed to doing settings in IIS
Note: I understand that multi-tenancy is a big word and should not be used for such a simple scenario but my requirements are simple. i would just run same instance of the application for each company with no extension points. This question could also be stated as how can i create subdomains programmatically.
回答1:
You have to configure IIS (and perhaps DNS) correctly for this work. For example, all your sub-domains should handle by the designated web-site in IIS. Typically, you may configure IIS to handle all host-headers if there are only single web site but in case of multiple web-sites, IIS is typically configured to differ by host header. So getting this configuration right is the important part for you.
Once you reach to the correct web-site, resource handling will be done by IIS meta-base. So in this case, it would re-direct to configured default resource for the site. If resource-name is present then extension (htm, aspx) will decide the handling. Aspx extensions will handled by ASP.NET and then all you need to do is to look up current host header and take a decision accordingly. For example,
protected void Page_Load(object sender, EventArgs e)
{
if (request.Url.HostNameType == UriHostNameType.Dns)
{
var hostParts = Request.Url.Host.Split('.');
// you may validate if sub-domain name is present or not
lblMessage.Text = "This is default page for " + hostParts[0];
}
}
来源:https://stackoverflow.com/questions/9565961/multitenancy-in-asp-net-web-forms