+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Absolute path for images in news items

Absolute path for images in news items

2 posts, 0 answered
  1. Nischal Jain
    Nischal Jain avatar
    8 posts
    Registered:
    19 Apr 2010
    21 Mar 2011
    Link to this post
    Hello,

    How do I resolve image url in an news item into an absolute URL, so that I could use the news item content in multiple pages in different applications?

    Thanks in advance.

    Nishchal
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    28 Mar 2011
    Link to this post
    Hi Nischal Jain,

    to get an absolute url from a relative url, you should use VirtualPathUtility.ToAbsolute(string relativeUrl) method. This, however, will return you something like this:

    /myimages/cats/cat1.jpg

    Now, in order to make this URL usable outside of your application, you can make a simple class like follows:

    /// <summary>
    /// Provides utility methods for working with urls.
    /// </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">Any Url, either App relative or fully qualified</param>
        /// <param name="forceHttps">if true forces the url to use https</param>
        /// <returns></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);
     
            Uri originalUri = HttpContext.Current.Request.Url;
            newUrl = (forceHttps ? "https" : originalUri.Scheme) +
                     "://" + originalUri.Authority + newUrl;
     
            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">Any Url, either App relative or fully qualified</param>
        /// <returns></returns>
        public static string ResolveServerUrl(string serverUrl)
        {
            return ResolveServerUrl(serverUrl, false);
        }
    }

    I hope this answers your question.

    Kind regards,
    Ivan
    the Telerik team
2 posts, 0 answered