Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Url.Content() or ResolveUrl() in my MVC views?

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?

like image 946
Michael Haren Avatar asked Mar 10 '10 15:03

Michael Haren


3 Answers

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.

like image 119
Levi Avatar answered Nov 09 '22 17:11

Levi


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

like image 14
Darin Dimitrov Avatar answered Nov 09 '22 19:11

Darin Dimitrov


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

like image 12
c.sokun Avatar answered Nov 09 '22 18:11

c.sokun