21

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?

Michael Haren
  • 105,752
  • 40
  • 168
  • 205

3 Answers3

30

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.

Levi
  • 32,628
  • 3
  • 87
  • 88
14

Url.Content is more MVCish as it is the normal. ResolveUrl has been around since the beginning of ASP.NET.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
12

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 :)

c.sokun
  • 1,622
  • 4
  • 25
  • 40