2012-06-06 15 views
5

Ich versuche, den physischen Stamm und relativen Stamm meiner Anwendung im Speicher beim Start der Anwendung zu speichern.Erhalten relative Stamm in Global.Asax

Ich habe den physischen Pfad wie folgt:

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

Aber ich kann den relativen Pfad nicht bekommen. Ich habe den folgenden Code, der funktioniert, aber es erfordert es in einem page Objekt gesetzt werden:

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

Dank

Antwort

1

In Global.asax fügen Sie den folgenden Code ein:

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

Um Um einen relativen Pfad innerhalb von application_start zu erhalten, verwenden Sie folgendes:

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