Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So, here are my requirements. I am building a multi-tenant site where companies after they sign up get their own subdomain.
Assume that my business is called Email Jobs Inc with domain www.emailjobs.com. This site is hosted in a shared ISP environment so I have very limited access to IIS etc.

Just to clarify, here is an example of what I need:

company1 signs up and they get this url company1.emailjobs.com
company2 signs up and they get this url company2.emailjobs.com
company3 signs up and they get this url: company3.emailjobs.com

My understanding is that there is a DNS and IIS component to this. That is, I will need to change an entry in a DSN server and then also a binding in IIS. All this is very tricky, because of non-static IP addresses, etc. etc.

However, another option I heard of is to ask your ISP to setup an IIS wildcard option where *.emailjobs.com goes straight to my ASP.NET MVC server. There I can parse the URL, extract the subdomain and I would be in business! Right?

Could someone confirm that this is possible and correct?

I realize that another option I have is to build urls like this: www.emailjobs.com/company1 etc, but that would be far less elegant.
Any feedback would be greatly appreciated...

Best.
Posted
Updated 20-Nov-15 22:05pm
v3
Comments
Krunal Rohit 21-Nov-15 2:09am    
Don't shout! Caps are considered for shouting.

-KR
Suvendu Shekhar Giri 21-Nov-15 3:00am    
ok. what have you tried so far?
heresanjay01 21-Nov-15 3:51am    
i try with link but still this link create website but create like sub domain
http://stackoverflow.com/questions/1286831/programatically-create-a-web-site-in-iis-using-c-sharp-and-set-port-number

1 solution

Yes, the solution you have proposed is possible, and you can use sub-domains rather than sub-directories in the scenario you have described (e.g. company.emailjobs.com rather than www.emailjobs.com/company).

You can configure IIS with a wildcard sub-domain, so that all sub-domains bind to a single web site, and then use a regular expression in your code to determine the text for the sub-domain in an HTTP Request. Your regular expression might look something like this:

^(?<subdomain>\w+)\.emailjobs\.com$</subdomain>
 
Share this answer
 
Comments
heresanjay01 22-Nov-15 23:58pm    
thanking you sir.
i use this code ...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Administration;
using Microsoft.Web.Administration;
using System.IO;


public partial class _Default : System.Web.UI.Page
{

private const string SERVER_IP = "192.168.111.112";// put your ip address
private const int PORT = 80;
private const string WEB_DOMAIN_PATH = @"F:\\web\domains\{0}\";

//Live server
//private const string SERVER_IP = "192.168.111.111";

protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["user"]))
{

try
{
string username = Request.QueryString["user"];
string status = CreateUserSite(username, "codeproject.com");//change your Domain id

Response.Write(status);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
else
{
Response.Write("user parameter not supplied");
}


}


private string CreateUserSite(string user, string domain)
{


string path = string.Format(WEB_DOMAIN_PATH, domain);

string userpath = path + user;

string userUrl = user + "." + domain;

using (ServerManager serverManager = new ServerManager())
{

bool siteExists = false;
int number = serverManager.Sites.Where(p => p.Name.ToLower().Equals(userUrl.ToLower())).Count();

if (number == 0)
{
siteExists = false;
}
else
{
siteExists = true;
}

if (!siteExists)
{

//create user directory
Directory.CreateDirectory(userpath);

//copy every files from a-base to a new created folder
FileInfo[] d = new DirectoryInfo(path + @"\a-base").GetFiles();
foreach (FileInfo fi in d)
{
File.Copy(fi.FullName, userpath + @"\" + fi.Name, true);
}

//create a directory
Directory.CreateDirectory(userpath + @"\swfobject");

FileInfo[] d1 = new DirectoryInfo(path + @"\a-base\swfobject").GetFiles();
foreach (FileInfo fi in d1)
{
File.Copy(fi.FullName, userpath + @"\swfobject\" + fi.Name, true);
}



//create site
Site mySite = serverManager.Sites.Add(userUrl, path + user, PORT);
mySite.ServerAutoStart = true;
mySite.Applications[0].ApplicationPoolName = domain;

//create bindings
mySite.Bindings.Clear();
mySite.Bindings.Add(string.Format("{0}:{2}:{1}", SERVER_IP, userUrl, PORT ), "http");
mySite.Bindings.Add(string.Format("{0}:{2}:www.{1}", SERVER_IP, userUrl, PORT), "http");


Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection httpLoggingSection = config.GetSection("system.webServer/httpLogging", userUrl);
httpLoggingSection["dontLog"] = true;

serverManager.CommitChanges();

// ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('" + userUrl + " created');", true);

}
else
{
//ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "alert('user exists. Please use other name');", true);
throw new Exception("user exists. Please use other name");
}


return userUrl + " has been successfully created";
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900