Returns a site relative HTTP path from a partial path starting out with a ~. : Web Client « Network « C# / C Sharp






Returns a site relative HTTP path from a partial path starting out with a ~.

      
#region Using directives

using System;
using System.Web;

#endregion

namespace MvcSiteMapProvider.External
{
    /// <summary>
    /// UrlUtilities class. See http://www.west-wind.com/Weblog/posts/154812.aspx for more information.
    /// </summary>
    public static class UrlUtilities
    {
        /// <summary>
        /// Returns a site relative HTTP path from a partial path starting out with a ~.
        /// Same syntax that ASP.Net internally supports but this method can be used
        /// outside of the Page framework.
        /// Works like Control.ResolveUrl including support for ~ syntax///
        /// but returns an absolute URL.
        /// </summary>
        /// <param name="originalUrl">Any Url including those starting with ~</param>
        /// <returns>Relative url</returns>
        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;
        }

        /// <summary>
        /// This method returns a fully qualified absolute server Url which includes
        /// the protocol, server, port in addition to the server relative Url.
        /// Works like Control.ResolveUrl including support for ~ syntax
        /// but returns an absolute URL.
        /// </summary>
        /// <param name="serverUrl">The server URL.</param>
        /// <param name="forceHttps">if true forces the url to use https</param>
        /// <returns>Fully qualified absolute server url.</returns>
        public static string ResolveServerUrl(string serverUrl, bool forceHttps)
        {
            // Is it already an absolute Url?
            if (serverUrl.IndexOf("://") > -1)
                return serverUrl;

            // Start by fixing up the Url an Application relative Url
            string newUrl = ResolveUrl(serverUrl);

            // Due to URL rewriting, cloud computing (i.e. Azure)  
            // and web farms, etc., we have to be VERY careful about what  
            // we consider the incoming URL.  We want to see the URL as it would  
            // appear on the public-facing side of the hosting web site.  
            // HttpRequest.Url gives us the internal URL in a cloud environment,  
            // So we use a variable that (at least from what I can tell) gives us  
            // the public URL:
            Uri originalUri = null;
            if (HttpContext.Current.Request.Headers["Host"] != null)
            {
                string scheme = HttpContext.Current.Request.Headers["HTTP_X_FORWARDED_PROTO"] 
                    ?? HttpContext.Current.Request.Url.Scheme;
                originalUri = new Uri(scheme + Uri.SchemeDelimiter + HttpContext.Current.Request.Headers["Host"]);
            }
            else
            {
                originalUri = HttpContext.Current.Request.Url;
            }

            newUrl = (forceHttps ? "https" : originalUri.Scheme) +
                     "://" + originalUri.Authority + newUrl;

            if (newUrl.EndsWith("//"))
            {
                newUrl = newUrl.Substring(0, newUrl.Length - 2);
            }

            // Strip off the application root
            newUrl = new Uri(newUrl).GetLeftPart(UriPartial.Authority);

            return newUrl;
        }

        /// <summary>
        /// This method returns a fully qualified absolute server Url which includes
        /// the protocol, server, port in addition to the server relative Url.
        /// It work like Page.ResolveUrl, but adds these to the beginning.
        /// This method is useful for generating Urls for AJAX methods
        /// </summary>
        /// <param name="serverUrl">The server URL.</param>
        /// <returns>Fully qualified absolute server url.</returns>
        public static string ResolveServerUrl(string serverUrl)
        {
            return ResolveServerUrl(serverUrl, false);
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Basic WebClient
2.Save web page from HttpWebResponse
3.Displays the resource specified
4.My Web Client
5.Handle network exceptionsHandle network exceptions
6.Use WebClient to download information into a fileUse WebClient to download information into a file
7.Use LastModifiedUse LastModified
8.Examine the headersExamine the headers
9.Access the InternetAccess the Internet
10.Reading Web Pages
11.NetworkCredential Cache Test
12.NetworkCredential test
13.Download Data Test
14.Download File Test
15.Web GetWeb Get
16.Web Client Upload Values Test
17.Web Client Upload Data Test 2
18.Web Client Response Headers Test
19.Web Client Open Write Test
20.Web Client Open Read Test
21.Gets or sets the network credentials that are sent to the host and used to authenticate the request.
22.Download String
23.Web Downloader
24.Fetches a web page
25.Http Get
26.Get Web Text
27.HTTP error code
28.Stores/save an image from the web to disk.