Generating full Content URL in MVC

Ever needed to provide a full and absolute URL to a script, css or image in MVC?

Well, you may find that Url.Content helper is quite handy as it does return an absolute path of the application just as it was done in WebForms so that something like the following would occur if used anywhere within the application:

@Url.Content(“~/Scripts/javascriptfile.js”)

Converts to /SomeVirtualPath/Scripts/javascriptfile.js down to the browser (which is really a relative path rather than an absolute).

However, what if you wanted to return the full and absolute URL: “http://domainname.com/VirtualPath/Scripts/javascriptfile.js” by specifying a relative path on the server. After searching google for quite a while I came to a realisation that this must be one of the least demanded answers for ASP.Net.

So,  here’s something I put together:

public static class UrlHelperExtensions
    {
        public static string ContentAbsUrl(this UrlHelper url, string relativeContentPath)
        {
            Uri contextUri = HttpContext.Current.Request.Url;

            var baseUri = string.Format("{0}://{1}{2}", contextUri.Scheme,
               contextUri.Host, contextUri.Port == 80 ? string.Empty : ":" + contextUri.Port);

            return string.Format("{0}{1}", baseUri, VirtualPathUtility.ToAbsolute(relativeContentPath));
        }
    }

So calling it from a razor view:

<script type="text/javascript" src="@Url.ContentAbsUrl("~/Scripts/magic.js")"></script>

7 Comments

  1. Bruno says:

    That was helpful, thanks !

    1. Dejan says:

      Happy to help!

  2. sim says:

    You saved me on a Friday evening. many thanks

  3. Michael says:

    This isn’t how extension methods work. You’re calling an extension method on the class and not an instance of the class

  4. Yorbenys says:

    Excelent post 😉

Leave a Comment