User987
User987

Reputation: 3823

Creating subdomains using C#

I have a question in regards to creating subdomains using C#. So the idea behind this is that a user can create a subdomain on our web application like following:

username.example.com 

Or

  username2.example.com

I'm familiar with static/dynamic routes. I'm guessing this can be done with one of those or with virtual directory?

How can this be performed in C# and .NET?

Upvotes: 2

Views: 2820

Answers (1)

Aristos
Aristos

Reputation: 66641

First let’s say that you have a dedicate IP to your site to avoid the binding on the IIS

Second step, let say that you use the BIND for dns, so you open the correct txt file and adds or remove there the correct subdomain with an update to the time stamp, and a restart the BIND service with a command call from asp.net… (you need to know some basic for DNS, and give access to the pool to been able to read/write there)

Then you can read on the very first call on global.asax Application_BeginRequest the

HttpContext.Current.Request.Path

That contains the subdomain as well, and translate it using the

HttpContext.Current.RewritePath

To something like

username.example.com  -> example.com/users.aspx?userid=123456

This is the general idea all together (this is on global.asax)...

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string sThePathToReWrite = sFindTheRealPath();

    if (sThePathToReWrite != null){
        HttpContext.Current.RewritePath(sThePathToReWrite, false);
    }       
}

string sFindTheRealPath()
{
    string strCurrentPath = HttpContext.Current.Request.Path;   

    // analyze the strCurrentPath 
    // find for example the userid from the url
    // and return the translate one - if not find anything return null
    return "example.com/users.aspx?userid=" + userid        
}

Upvotes: 2

Related Questions