たいのですが解決ASP.NET "~"アプリへのパスがサイトのルートな制御については?

StackOverflow https://stackoverflow.com/questions/2589542

  •  25-09-2019
  •  | 
  •  

質問

思いを解決する"~/う"内外のページの文脈など。asax(HttpApplication),HttpModule,HttpHandler。できな解決方法に特有のコントロール(およびページ上)。

と思いこのアプリにおいては、広い分野での知識できる地図のこのページのコンテキストありませんか?●少なくともこうあるべきで分解可能その他の状況で、どこのアプリのルートすることが知られている。

更新:その理由は私のこだわり"~"パス。設定ファイルをため、その解決の非制御ざいます。

更新2: ようにしているのを解決し、サイトのルートなど。解決す(..)URL行動しないファイルシステムのパスです。

役に立ちましたか?

解決

ここに答えがあります: ASP.Net:システムの使用共有/静的関数で.Web.UI.Control.ResolveUrl()

string absoluteUrl = VirtualPathUtility.ToAbsolute("~/SomePage.aspx");

他のヒント

あなたは直接HttpContext.Currentオブジェクトにアクセスすることにより、それを行うことができます:

var resolved = HttpContext.Current.Server.MapPath("~/whatever")
注意すべき点は、HttpContext.Currentだけで、実際の要求のコンテキストで非nullになるだろうということです。それは、例えば、Application_Stopイベントでは使用できません。

での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;
    }
}

私はこの吸盤をデバッグしていないが、コントロールの.NETフレームワークの外での解決方法を見つけるの欠如のためのマニュアルソリューションとして、私たちがそれを投げています。

このは "〜/何でも" 私にとってます。

に仕事をしました
/// <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;
}
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));
}

代わりにMapPathのを使用しての、System.AppDomain.BaseDirectoryを使用してみてください。ウェブサイトの場合、これはあなたのウェブサイトのルートでなければなりません。そして、あなたは「〜」なしMapPathのに渡すつもりだったものは何でもしてSystem.IO.Path.Combineを行います。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top