开发者

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

开发者 https://www.devze.com 2022-12-22 07:19 出处:网络
When building code like this: <script type=\"text/javascript\" src=\"<%=ResolveUrl(\"~/js/js.js\")%>\"></script开发者_运维问答>

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

精彩评论

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

关注公众号