2012-06-06 17 views
5

Sto cercando di memorizzare la radice fisica e la radice relativa della mia applicazione in memoria all'avvio dell'applicazione.Ottenere radice relativa in Global.Asax

ho fatto il percorso fisico in questo modo:

//Get the physical root and store it 
    Global.ApplicationPhysicalPath = Request.PhysicalApplicationPath; 

ma non riesco a ottenere il percorso relativo. Ho il seguente codice che funziona, ma richiede di essere messo in un oggetto page:

Global.ApplicationRelativePath = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "") + Page.ResolveUrl("~/"); 

Grazie

risposta

1

In Global.asax aggiungere il seguente codice:

private static string ServerPath { get; set; } 

protected void Application_BeginRequest(Object sender, EventArgs e) 
    { 
     ServerPath = BaseSiteUrl; 
    } 

    protected static string BaseSiteUrl 
    { 
     get 
     { 
      var context = HttpContext.Current; 
      if (context.Request.ApplicationPath != null) 
      { 
       var baseUrl = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/') + '/'; 
       return baseUrl; 
      } 
      return string.Empty; 
     } 
    } 
3

Al fine per ottenere un percorso relativo all'interno di application_start, utilizzare quanto segue:

protected void Application_Start(object sender, EventArgs e) 
{ 
    string path = Server.MapPath("/"); 
} 
Problemi correlati