Content HTML Helper in MVC3 Razor

  1. The Content helper is used to specify absolute path.
  2. This helper is particularly helpful because it can convert a relative application path to an absolute application path.

Example :

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

Rendered HTML :

    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>

Using a tilde as the first character in the parameter you pass to the Content helper will let the helper generate the proper URL no matter where your application is deployed.

      Without tilde sign the URL could break if you moved the application up or down the virtual directory tree.


Post a Comment