Getting full URL from URL with tilde(~) sign

后端 未结 4 1971
醉酒成梦
醉酒成梦 2020-12-08 13:45

I am trying to get a typical asp.net url starting with the tilde sign (\'~\') to parse into a full exact url starting with \"http:\"

I have this string \"~/PageB.asp

相关标签:
4条回答
  • 2020-12-08 13:47

    Try out

    System.Web.VirtualPathUtility.ToAbsolute("yourRelativePath"); 
    

    There are various ways that are available in ASP.NET that we can use to resolve relative paths to a resource on the server-side and making it available on the client-side. I know of 4 ways -

     1) Request.ApplicationPath
     2) System.Web.VirtualPathUtility
     3) Page.ResolveUrl
     4) Page.ResolveClientUrl
    

    Good article : Different approaches for resolving URLs in ASP.NET

    0 讨论(0)
  • 2020-12-08 13:59

    This method looks the nicest to me. No string manipulation, it can tolerate both relative or absolute URLs as input, and it uses the exact same scheme, authority, port, and root path as whatever the current request is using:

    private Uri GetAbsoluteUri(string redirectUrl)
    {
        var redirectUri = new Uri(redirectUrl, UriKind.RelativeOrAbsolute);
    
        if (!redirectUri.IsAbsoluteUri)
        {
            redirectUri = new Uri(new Uri(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath), redirectUri);
        }
    
        return redirectUri;
    }
    
    0 讨论(0)
  • 2020-12-08 14:00

    If you're in a page handler you could always use the ResolveUrl method to convert the relative path to a server specific path. But if you want the "http://www.yourserver.se" part aswell, you'll have to prepend the Request.Url.Scheme and Request.Url.Authority to it.

    0 讨论(0)
  • 2020-12-08 14:08
    string.Format("http://{0}{1}", Request.Url.Host, Page.ResolveUrl(relativeUrl));
    
    0 讨论(0)
提交回复
热议问题