2

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?

5
  • 1
    Note you'll also have to handle this on the DNS side, or direct any non-matching A record requests to your server. Commented Jun 13, 2018 at 11:18
  • @john yes I supposed so... I'd just need some good answer from someone to clarify the things a bit for everyone who wants to know more on this topic... Commented Jun 13, 2018 at 11:19
  • 1
    I don't know enough about the IIS side of this to give an answer, but just wanted to make sure you don't forget :) Commented Jun 13, 2018 at 11:20
  • @john thx for that =) I wonder if there's someone who'll reply soon Commented Jun 13, 2018 at 11:24
  • Anyone guys ? =) Commented Jun 13, 2018 at 11:33

1 Answer 1

2

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        
}
Sign up to request clarification or add additional context in comments.

5 Comments

Okay hold on I'm gonna check it out =)
@User987 I have implement that scenario but I do not have some specific source code to show you because it’s a complex one that’s include database read and setup. But this is the general idea, the basic steps.
could you post the source code for the url rewrite part at least ? =) I'll find a way for db read/write setup
@User987 all the difficult code is to analyze the Request.Path and extract the "username"... then you just call this HttpContext.Current.RewritePath("example.com/users.aspx?userid=12345", false);
@User987 I have copy/paste the base code that I have

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.