Quantum Loop Gravity

Tuesday, July 18, 2006

ASP.Net Redirect based on referer

This code snippet is for anyone that is trying to do something like handle multiple domains on one
hosting account. Just checking for the server variable HTTP_REFERER is not enough, as they
can type in the url directly in their browser, or select a link from a bookmark. So, this will check
those conditions first:

if(!Page.IsPostback)
{
ViewState["URLReferrer"] = Request.UrlReferrer;
}

// when you want to direct
if(ViewState["URLReferrer"] != null)
{
Response.Redirect(Convert.ToString(ViewState["URLReferrer"]))'
}
else
{
Response.Redirect("~");
// ~ redirect to current application's default page
}