When building code like this:
<script type="text/javascript" src="<%=ResolveUrl("~/js/js.js")%>"></script>
or
<input type="image" src="<%=ResolveUrl("~/img/submit.png")%>" />
Should I use Url.Content
or ResolveUrl()
? What's the difference?
If you're using IIS URL Rewriting within your MVC application, e.g. internally treating http://yoursubdomain.example.com/MyController/MyAction as http://hosted.example.com/yoursubdomain/MyController/MyAction, Url.Content() will generate a correct subdomain-relative link. ResolveUrl() will generate an incorrect link in this situation.
Url.Content
is more MVCish as it is the normal. ResolveUrl has been around since the beginning of ASP.NET.
I prefer to capture site root into local variable and reuse it
<% var siteroot = Url.Content("~/") %>
<script type="text/javascript" src="<%: siteroot %>Script/jquery-1.4.1.js"></script>
<script type="text/javascript" src="<%: siteroot %>Script/jquery.validate.js"></script>
It should save a few ms :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With