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
  • 101,522
  • 39
  • 162
  • 203

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,493
  • 3
  • 85
  • 88
14

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

Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
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,572
  • 4
  • 24
  • 40