如何获取相对uri的绝对uri?

发布于 2024-10-30 18:38:04 字数 176 浏览 0 评论 0原文

我有一个页面,其相对路径是“~/pages/mypage.aspx”。

我尝试使用 VirtualPathUtility.ToAbsolute(@"~/pages/mypage.aspx") 希望获得绝对 uri,但它只返回相同的相对路径。

如何从相对 uri 中获取绝对 uri?

谢谢,

I have a page whose relative path is "~/pages/mypage.aspx".

I tried using VirtualPathUtility.ToAbsolute(@"~/pages/mypage.aspx") hoping to get the absolute uri but it only returns the same relative path.

How could I get the absolute uri from this relative uri?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-11-06 18:38:04

我用了这个方法:

private static string GetAbsoluteUrl(string relativeUrl)
    {
        var applicationUrl = String.Empty;

        // remove ~
        if (relativeUrl.StartsWith("~"))
        {
            applicationUrl = relativeUrl.Substring(1);
        }

        applicationUrl = (HttpContext.Current.Request.ApplicationPath + applicationUrl)
            .Replace("//", "/");

        var baseUrl = String.Format("{0}://{1}:{2}",
                                    HttpContext.Current.Request.Url.Scheme,
                                    HttpContext.Current.Request.Url.Host,
                                    HttpContext.Current.Request.Url.Port);

        return baseUrl + applicationUrl;
    }

I used this method:

private static string GetAbsoluteUrl(string relativeUrl)
    {
        var applicationUrl = String.Empty;

        // remove ~
        if (relativeUrl.StartsWith("~"))
        {
            applicationUrl = relativeUrl.Substring(1);
        }

        applicationUrl = (HttpContext.Current.Request.ApplicationPath + applicationUrl)
            .Replace("//", "/");

        var baseUrl = String.Format("{0}://{1}:{2}",
                                    HttpContext.Current.Request.Url.Scheme,
                                    HttpContext.Current.Request.Url.Host,
                                    HttpContext.Current.Request.Url.Port);

        return baseUrl + applicationUrl;
    }
谈下烟灰 2024-11-06 18:38:04
 public static string ResolveUrl(string originalUrl)
    {
        if (originalUrl == null)
            return null;

        // *** Absolute path - just return
        if (originalUrl.IndexOf("://") != -1)
            return originalUrl;

        // *** Fix up image path for ~ root app dir directory
        if (originalUrl.StartsWith("~"))
        {
            string newUrl = "";
            if (HttpContext.Current != null)
                newUrl = HttpContext.Current.Request.ApplicationPath +
                      originalUrl.Substring(1).Replace("//", "/");
            else
                // *** Not context: assume current directory is the base directory
                throw new ArgumentException("Invalid URL: Relative URL not allowed.");

            // *** Just to be sure fix up any double slashes
            return newUrl;
        }

        return originalUrl;
    }

摘自http://www.west-wind.com/weblog/posts/154812 .aspx

 public static string ResolveUrl(string originalUrl)
    {
        if (originalUrl == null)
            return null;

        // *** Absolute path - just return
        if (originalUrl.IndexOf("://") != -1)
            return originalUrl;

        // *** Fix up image path for ~ root app dir directory
        if (originalUrl.StartsWith("~"))
        {
            string newUrl = "";
            if (HttpContext.Current != null)
                newUrl = HttpContext.Current.Request.ApplicationPath +
                      originalUrl.Substring(1).Replace("//", "/");
            else
                // *** Not context: assume current directory is the base directory
                throw new ArgumentException("Invalid URL: Relative URL not allowed.");

            // *** Just to be sure fix up any double slashes
            return newUrl;
        }

        return originalUrl;
    }

Taken from http://www.west-wind.com/weblog/posts/154812.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文