Creating DiscountASP subdomains in C#

Discussion in 'Getting started' started by Rob Croll, Sep 21, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. DiscountASP has attracted a lot of ASP.NET developers because of its support for ASP.NET. For a lot of ASP.NET developers creating subdomains at DiscountASP is slightly different and the documentation isn’t clear to us newbies. So hopefully this post will make supporting subdomains in Visual Studio easier to understand.

    I assume you have already created an ASP.NET project in Visual Studio using C# and have successfully published it to your DiscountASP website. And now you want to create subdomains to point to a particular subfolder or page.

    You need to do two things to get subdomains working.

    Step 1
    Log on to your DiscountASP Control Panel and click the “Order Addons” menu on the left-hand side. Then order the “Unlimited Subdomain” option. Give it 24 hours, DiscountASP will email you.

    Step 2
    Open up your project in Visual Studio and select the default page. It is normally called Default.aspx and it is in the root directory of your project. Visual Studio supports what is called “code behind” which helps separate your markup language from your code. See MSDN for more information

    Open the code behind (Default.cs) file and add the following method.

    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
    
       string subDomain = "subdomain";
       string subDomainUri = @"/MySubdomainFolder/Default.aspx";
    
       if (Request.ServerVariables["SERVER_NAME"].ToLower().Contains(subDomain))
            Response.Redirect(subDomainUri);
    }
    
    If your subdomain is foo as in foo.mydomain.com then the subDomain string variable should be set to “foo”, in lower case. The subDomainUri variable represents the file, within your project, you wish to redirect to.

    Now recompile and publish. Once published, just to make sure, open up a new web browser session and navigate to your new subdomain.

    subDomain.MyWebsite.com

    If that didn’t work or you want to check that the subDomainUri variable is pointing to the right place you can add the following code within the Page_Load method.

    Code:
    if (Request.ServerVariables["SERVER_NAME"].Contains("localhost"))
                Response.Redirect(Request.ApplicationPath + subDomainUri);
    
    Add a break point to the code and run in debug mode. If you didn’t hit the break point there is something wrong with your code.
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    thanks for posting.
     
  3. thanks

    thanks, this was very helpful to me, just what I was looking for :) and in plain english
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page