2010-04-07 59 views
13

我想从非页面上下文(如Global.asax(HttpApplication),HttpModule,HttpHandler等)中解析“〜/ whatever”,但只能找到控件(和页面)特定的解析方法。如何在不存在控件的情况下解决ASP.NET网站根目录的“〜”应用程序路径?

我认为应用程序应该有足够的知识能够将这个外部映射到页面上下文中。没有?或者至少对我来说它是有道理的,在其他情况下应该可以解决,无论应用程序根源在哪里。

更新:原因是我在web.configuration文件中粘住了“〜”路径,并且想从上述非控制场景中解决它们。

更新2:我试图将它们解析为网站根,例如Control.Resolve(..)URL行为,而不是文件系统路径。

+0

重复的:http://stackoverflow.com/questions/26796/asp-net-using-system-web-ui-control-resolveurl-in- a-shared-static-function – 2010-04-07 03:21:06

回答

1

您可以通过直接访问HttpContext.Current对象做到这一点:

var resolved = HttpContext.Current.Server.MapPath("~/whatever") 

要注意的一点是,HttpContext.Current只会在实际申请的情况下非null。例如,在Application_Stop事件中不可用。

+3

我更新了这个问题,因为我试图解析到一个URL,而不是文件系统。 – 2010-04-07 02:13:37

0

我还没有调试这个吸盘,但我把它作为一个手动解决方案扔在那里,因为没有在控件的.NET框架中找到Resolve方法。

这确实对我的“〜/ whatever”有用。

/// <summary> 
/// Try to resolve a web path to the current website, including the special "~/" app path. 
/// This method be used outside the context of a Control (aka Page). 
/// </summary> 
/// <param name="strWebpath">The path to try to resolve.</param> 
/// <param name="strResultUrl">The stringified resolved url (upon success).</param> 
/// <returns>true if resolution was successful in which case the out param contains a valid url, otherwise false</returns> 
/// <remarks> 
/// If a valid URL is given the same will be returned as a successful resolution. 
/// </remarks> 
/// 
static public bool TryResolveUrl(string strWebpath, out string strResultUrl) { 

    Uri uriMade = null; 
    Uri baseRequestUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)); 

    // Resolve "~" to app root; 
    // and create http://currentRequest.com/webroot/formerlyTildeStuff 
    if (strWebpath.StartsWith("~")) { 
     string strWebrootRelativePath = string.Format("{0}{1}", 
      HttpContext.Current.Request.ApplicationPath, 
      strWebpath.Substring(1)); 

     if (Uri.TryCreate(baseRequestUri, strWebrootRelativePath, out uriMade)) { 
      strResultUrl = uriMade.ToString(); 
      return true; 
     } 
    } 

    // or, maybe turn given "/stuff" into http://currentRequest.com/stuff 
    if (Uri.TryCreate(baseRequestUri, strWebpath, out uriMade)) { 
     strResultUrl = uriMade.ToString(); 
     return true; 
    } 

    // or, maybe leave given valid "http://something.com/whatever" as itself 
    if (Uri.TryCreate(strWebpath, UriKind.RelativeOrAbsolute, out uriMade)) { 
     strResultUrl = uriMade.ToString(); 
     return true; 
    } 

    // otherwise, fail elegantly by returning given path unaltered.  
    strResultUrl = strWebpath; 
    return false; 
} 
0
public static string ResolveUrl(string url) 
{ 
    if (string.IsNullOrEmpty(url)) 
    { 
     throw new ArgumentException("url", "url can not be null or empty"); 
    } 
    if (url[0] != '~') 
    { 
     return url; 
    } 
    string applicationPath = HttpContext.Current.Request.ApplicationPath; 
    if (url.Length == 1) 
    { 
     return applicationPath; 
    } 
    int startIndex = 1; 
    string str2 = (applicationPath.Length > 1) ? "/" : string.Empty; 
    if ((url[1] == '/') || (url[1] == '\\')) 
    { 
     startIndex = 2; 
    } 
    return (applicationPath + str2 + url.Substring(startIndex)); 
} 
+0

对于同一个问题,问题2的答案有什么意义? – 2017-02-12 08:50:24

0

而不是使用的MapPath的,请尝试使用System.AppDomain.BaseDirectory。对于一个网站,这应该是你网站的根源。然后做一个System.IO.Path.Combine与你将要传递给MapPath而没有“〜”的任何东西。

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; 
    } 
} 
相关问题