How to Create Dynamic Sub-domain with ASP.NET? - asp.net

How can I create a subdomain in an asp.net C# application? I am working with an asp.net portal.
I have my site which redirects all *.domain.com calls to domain.com. What I want to acheive is that first when the user enters a dynamic subdomain name he should be directed to its home page like if user writes www.microsite1.domain.com, then the site should point to page ~/Microsite/Home.aspx?value=microsite1, and when the user accesses www.microsite1.domain.com/about.aspx then i should able to get the argument value1=about.

The first step is to place the subdomain name in the DNS Host Server. To do that you need to manipulate the dns files. For example if you use BIND as DNS server you go and open the text file that keep your DNS configuration eg: "c:\program files\dns\var\mysite.com" and there you add a line as
subdomain.mysite.com. IN A 111.222.333.444
Also you change the ID of the file to give a message to BIND to update the subdomains.
Second step is to redirect the new subdomain to the correct directory. You do that on the protected void Application_BeginRequest(Object sender, EventArgs e) on Global.asax using rewritepath
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.Host.StartsWith("subdomain."))
{
// here you need to find where to redirect him by reading the url
// and find the correct file.
HttpContext.Current.RewritePath("/subdomain/" + Request.Path, false);
}
// .... rest code
}
Its not so easy, not so hard... maybe there are some more minor issues like permissions to write to dns. Also you need to know dns, read the manual about.

Related

subdomain url redirects to the main url

I developed a website in asp.net and hosted on windows server which does not provide me the wild card entry for the subdomain name. How can I write my url as e.g
http://subdomainName.DomainName.org?
I want to redirect the url with a subdomain to the main domain; so url "subdomainName.DomainName.org" should redirect to "DomainName.org", where my subdomain name is not fixed. The subdomain will be assigned to each user.
How can I achieve this?
The subdomain is part of the DNS server, and work together with the IIS setup.
So, you can NOT change the DNS setup from asp.net, neither the IIS setup. When a provider gives your the access to add extra sub-domains, what is actually do is that create new entries on the DNS entry, and then add map that to the IIS, so that sub-domains to look at your site. If your provider did not have give you a tool to add sub-domains, nether you can edit the DNS entries, then you can NOT add them from asp.net.
If you can add sub-domains then you can manipulate what you going to server and show on global.asax at Application_BeginRequest using the redirect or the Rewrite the path. For example:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var SubDomain = GetSubDomain(HttpContext.Current.Request.Url.Host);
if(!String.IsNullOrEmpty(SubDomain) && SubDomain != "www")
{
Response.Redirect("www.yourdomain.com", true);
return;
}
}
// from : http://madskristensen.net/post/Retrieve-the-subdomain-from-a-URL-in-C.aspx
private static string GetSubDomain(Uri url)
{
string host = url.Host;
if (host.Split('.').Length > 1)
{
int index = host.IndexOf(".");
return host.Substring(0, index);
}
return null;
}
Similar posts:
How to remap all the request to a specific domain to subdirectory in ASP.NET
Redirect Web Page Requests to a Default Sub Folder
How to refer to main domain without hard-coding its name?
Retrieve the subdomain from a URL in C#

Track each request to the website using HttpModule

I want to save each request to the website. In general I want to include the following information:
User IP, The web site url, user-if-exist, date-time.
Response time, response success-failed status.
Is it reasonable to collect the 1 and 2 in the same action? (like same HttpModule)?
Do you know about any existing structure that I can follow that track every request/response-status to the website?
The data need to be logged to sql server.
Look in your web server logs.
How about IIS logs? they already have all the data items you listed so far
In BeginRequest Method you need to write the following code.
protected void Application_BeginRequest(object sender, EventArgs e)
{
//String s = HttpContext.Current.Request.Path;
//HttpContext.Current.RewritePath("Login.aspx");
String referrer = HttpContext.Current.Request.UrlReferrer;
String sourceIP = HttpContext.Current.Request.UserHostAddress;
String browser = HttpContext.Current.Request.UserAgent;
}

At what point in the ASP.NET page/app life-cycle should I redirect a user based on his country?

We have a web app where we need to redirect customers from .com to .co.uk domains based on the geolocation of ther IP address. When/where should I do this?
The content will change slightly based on their location, but I think I can handle that OK. However, if anyone has any comments on the best way to handle this, I'd like to hear those comments as well.
I would use an HttpModule. It's the earliest place to do the redirection.
If you want to do this in application code, I would do this as early as the IP is available, in the Application_BeginRequest handler in global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
var ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//Redirect here...
}

URL redirection

When user write http://nextech.pk/ in the browser, I want to redirect to http://www.nextech.pk/ , So I want to embend www to the URL when user write nextech.pk in the browser
How can I accomplish this?
I think I need to write some rules in the web.cofig to accomplish this, but I don't know exactly
Thanks
It will be there as an option in the domain settings page where your domain is registered.
use nextech.pk or www.nextech.pk or both something like that.
Changing there will be the easy one.
just add index.html with:
<meta http-equiv="Refresh" content="0; http://www.example.com">
In the base page for your website or your Default.aspx file, check if the request url is prefixed with www. Otherwise redirect using Response.Redirect to the url prefixed with www.
I don't do this at the ASP.NET level, as it would fail on static content such as images. Simply add an additional website to IIS, with the host header value of all the URLs you wish to redirect FROM (e.g. example.com). On the "Home Directory" tab, click on "A redirection to a URL", and enter the URL you wish to redirect TO.
This allows you to enter http://example.com/foo.txt and end up at http://www.example.com/foo.txt
(NB. These instructions are for IIS 6, they'll vary slightly for IIS 7).
You might want to check with your domain name provider. You should be able to configure your hosting to automatically redirect from nextech.pk to www.nextech.pk.
But you could also do it in Global.asax or with an IHttpModule in the BeginRequest handler:
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication) sender;
if (!application.Request.Url.ToString().Contains("http://www."))
{
application.Response.Redirect(
application.Request.Url.ToString().Replace("http://", "http://www."));
}
}
Both of these methods would work with any request on your website.

Request.ServerVariables["SERVER_NAME"] is always localhost

I'm developing an ASP.NET 3.5 application with Visual Studio 2008.
My default page has some redirection code in the Page_Load method:
protected void Page_Load(object sender, EventArgs e)
{
string sname = Request.ServerVariables["SERVER_NAME"].ToLower();
if (sname.ToLower().Contains("intranet"))
{
Response.Redirect("/intranet/Default.aspx");
}
else if ((sname.ToLower().Contains("extranet")))
{
Response.Redirect("/extranet/Default.aspx");
}
else {
Response.Redirect("/web/Default.aspx");
}
}
I've modified my hosts file so that intranet and extranet redirect to my local machine.
127.0.0.1 intranet
127.0.0.1 extranet
I then type the URL http://extranet in my browser.
However, the problem is that the server variable value returned from Request.ServerVariables["SERVER_NAME"] is always "localhost" and not "extranet"
Any help on how to get the right value?
Many thanks
Request.ServerVariables["HTTP_HOST"] gets the value I was looking for :)
Youre right
You want to retrieve the full address of the website that the request came to. Do not use "SERVER_NAME", use "HTTP_HOST".
Read here,
http://www.requestservervariables.com/get-address-for-website
Server_Name returns the server's host name, DNS alias, or IP address as it would appear in self-referencing URLs
Why don't you use Request.URL?
Your host files only redirect the requests to a specific IP address - you cannot change the requesting machines name by editing them.

Resources