hi everybody
i want to create a weblog engine ! like blogspot.com . i want to create a subdomain for each of my users like a.blogspot.com and i even want to let users to have their own domain they want like mydomain.net . the only thing i dont know is how to create sub domains in asp.net using c#.net
how can i do this?
RobbinsonPosted Dec 26, 2013, 7:11 AM
The way to create sub domains is to use wild card domain in IIS, so every request to the top level domain and under. But IIS does not support Wild Card host headers.
The workaround to this is to configure website with No host header, then assign an IP address to the site.
Next step is to make sure your name resolution works for the wildcard query and reply with the correct IP address. If you using Microsoft DNS service, it won't allow you to create a '*' A record (assuming you already created the domain zone in DNS MMC), you need to do the following:
1) Navigate to %windir%\system32\dns\
2) Find the zone file. E.g. mydomain.com.dns, open it with Notepad
3) Add an entry. E.g.
* A IP.IP.IP.IP
4) Save the zone data file
5) Reload the zone data in DNS MMC.
Please note that by doing this, all * will response to the IP that you configured earlier. E.g. abc.mydomain.com, www.mydomain.com, K2k.mydomain.com and etc.
Now,in asp.net, create HttpModule, and check the domain name, etc and pull in user information based on the subdomain. Which will execute on every request.
The basic code for HttpModule looks like below :
public class MyModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string host = app.Request.Url.Host;
//Split host "." to get the subdomain user name.
// perform action based on the user information
}
}
Hope, this will help you out. Feel free to connect in case of any query.
please don't forget to mark as answer if this post helps you.