2010-04-07 8 views
13

Global.asax (HttpApplication), HttpModule, HttpHandler, vb. Gibi Sayfa dışı bağlamlardan "~/her neyse" çözümünü çözmek istiyorum, ancak yalnızca Denetimler (ve Sayfa) için özel olan bu Çözümleme yöntemlerini bulabilirim.ASP.NET "~" uygulama yollarını, mevcut bir Kontrol olmaksızın web sitesinin köküne nasıl çözebilirim?

Uygulamanın, Sayfa bağlamı dışında haritayı çizebilmek için yeterli bilgiye sahip olması gerektiğini düşünüyorum. Yok hayır? Ya da en azından benim için mantıklı, diğer durumlarda, uygulama kökü bilindiği yerde çözülebilir olmalıdır.

Güncelleştirme: Web.configuration dosyalarında "~" yollarına yapıştığım ve bunları yukarıda belirtilen denetim dışı senaryolardan çözmek istediğim için.

Güncelleştirme 2: Onları bir dosya sistemi yoluna değil, Control.Resolve (..) URL davranışı gibi web sitesinin köküne çözmeye çalışıyorum.

+0

Yinelenen: http://stackoverflow.com/questions/26796/asp-net-using-system-web-ui-control-resolveurl-in- a-shared-static-function –

cevap

1

doğrudan HttpContext.Current nesneyi erişerek bunu yapabilirsiniz: nota

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

Bir nokta HttpContext.Current sadece gerçek bir isteği kapsamında null olmayan olacak, olmasıdır. Örneğin, Application_Stop etkinliğinde mevcut değildir.

+3

Sorunu güncelledim çünkü dosya sistemine değil, bir URL'ye çözümlemeye çalışıyorum. –

0

Bu envanterde hata ayıklamadım, ancak Denetim dışında .NET Framework'te bir Çözüm yöntemi bulma konusunda elimizde bir çözüm olarak elimizde var.

Bu, benim için "~/her neyse" üzerinde çalışıyordu.

/// <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

Aynı soru için 2 sonrası cevapların anlamı nedir? –

0

, System.AppDomain.BaseDirectory kullanmayı deneyin. Bir web sitesi için bu, web sitenizin kökü olmalıdır. Sonra "~" olmadan MapPath'e geçecek ne ile bir System.IO.Path.Combine yapın. Global.asax eklemek yılında

1

aşağıdadır:

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; 
    } 
} 
İlgili konular