2012-06-06 22 views
5

Tôi đang cố lưu trữ gốc thực và gốc tương đối của ứng dụng trong bộ nhớ khi khởi động ứng dụng.Lấy gốc tương đối trong Global.Asax

tôi đã làm con đường vật lý như vậy:

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

Nhưng tôi không thể có được đường dẫn tương đối. Tôi có đoạn code sau đây mà làm việc, nhưng nó đòi hỏi nó phải được đặt trong một đối tượng page:

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

Cảm ơn

Trả lời

1

Trong Global.asax thêm đoạn mã sau:

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

Để để có được một đường dẫn tương đối trong application_start sử dụng như sau:

protected void Application_Start(object sender, EventArgs e) 
{ 
    string path = Server.MapPath("/"); 
} 
Các vấn đề liên quan