开发者

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜