2010-01-21 13 views

Respuesta

4
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(); 
    } 
} 
+0

Hola, he encontrado que el manejador PreRequest no existía en el Global.asax por lo que añade que a medida que se propone. Pero el evento no se activa en modo de depuración ... ¿Estoy haciendo algo diferente aquí? – OrElse

+0

¿Se activa cuando se cambia 'PreRequestHandlerExecute' en' BeginRequest'? –

+0

¡Yeap! BeginRequest se activa en todas las solicitudes – OrElse

9

Un par de cambios menores a la respuesta de Jan tengo trabajo para mí:

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower(); 
    if (currentUrl.StartsWith("http://mydomain")) 
    { 
     Response.Status = "301 Moved Permanently"; 
     Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain")); 
     Response.End(); 
    } 
} 

Los cambios eran utilizar el evento BeginRequest y para establecer CURRENTURL a HttpContext.Current.Request.Url en lugar de HttpContext .Current.Request.Path. Ver:

http://www.mycsharpcorner.com/Post.aspx?postID=40

Cuestiones relacionadas