2012-06-06 24 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("/"); 
} 
関連する問題