views:

154

answers:

1

Hello,

could i use the begin request of Global.asax to redirect everything,

from mydomain.domain to www.mydomain.domain?

If this one is true, how can i do that?

A: 
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
  string currentUrl = HttpContext.Current.Request.Path.ToLower();
  if(currentUrl.StartsWith("http://mydomain"))
  {
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
    Response.End();
  }
}
Jan Jongboom
Hello, i found that the PreRequest handler did not exist in the global.asax so i added it as you proposed. But the event is not fired in debug mode... I am doing something else wrong here?
Chocol8
Is it firing when you change `PreRequestHandlerExecute` into `BeginRequest`?
Jan Jongboom
Yeap! The BeginRequest fires in every request
Chocol8
Then move the inner piece to BeginRequest and you'd be fine.
Jan Jongboom
Request.Path only returns the virtual path, without the domain name. The above code will not work.
Alex Czarto