ASP.NET MVC : Url Helper Extension CSS, Javascript, Picture

29 May
2009

Here a url helper, maybe it can interests you :


public static class UrlHelperExtension
{
public static string Image(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("~/Content/Images/{0}",fileName));
}

public static string Stylesheet(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("~/Content/Css/{0}.css",fileName));
}

public static string Javascript(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("~/Content/Scripts/{0}.js", fileName));
}
}

And here is how I use it :

For CSS :


<link rel="stylesheet" type="text/css" href="<%= Url.Stylesheet("reset") %>" media="all" />

For javascript


<script src="<%= Url.Javascript("jquery-1.3.2.min") %>" type="text/javascript" language="javascript"></script>

Comment Form

top