2010-01-21 12 views

risposta

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

Ciao, ho scoperto che il gestore PreRequest non esisteva nel global.asax così ho aggiunto come avete proposto. Ma l'evento non viene attivato in modalità di debug ... Sto facendo qualcos'altro di sbagliato qui? – OrElse

+0

Si sta attivando quando si modifica 'PreRequestHandlerExecute' in' BeginRequest'? –

+0

Yeap! BeginRequest si attiva in ogni richiesta – OrElse

9

Un paio di piccoli cambiamenti alla risposta di Jan ottenuto che funziona per me:

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

I cambiamenti sono stati di utilizzare l'evento BeginRequest e per impostare CURRENTURL a HttpContext.Current.Request.Url invece di HttpContext .Current.Request.Path. Vedi:

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

Problemi correlati