2012-06-06 2 views
5

응용 프로그램 시작시 메모리에 내 응용 프로그램의 실제 루트와 상대 경로를 저장하려고합니다. Global.Asax의 상대 루트 얻기

나는 실제 경로 너무 좋아했다 :

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

하지만 상대 경로를 얻을 수 없습니다. 나는 작동하는 다음과 같은 코드를 가지고 있지만, 그것은 page 객체에 넣어 될 필요

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

감사

답변

1

의 Global.asax에 다음 코드를 추가합니다

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

위해를 application_start 내에서 상대 경로를 얻으려면 다음을 사용하십시오.

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