为什么要使用@ Url.Content [英] why use @Url.Content

查看:103
本文介绍了为什么要使用@ Url.Content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释我为什么要使用(或应该我?):

Can someone please explain why I should use (or should I?):

<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>

VS

 <script type="text/javascript" src="/Scripts/SomeScript.js"></script>

感谢

推荐答案

该片段 @ Url.Content(/脚本/ SomeScript.js)做绝对没问题,和仅相当于 /Scripts/SomeScript.js

The fragment @Url.Content("/Scripts/SomeScript.js") does absolutely nothing, and is equivalent to just /Scripts/SomeScript.js.

不过,对于以〜,它会转化的URL正确,程序相对URL路径,例如, @ Url.Content(〜/脚本/ SomeScript.js)可以转化为 /MyVirtualDirectory/Scripts/SomeScript.js ,如果部署Web应用程序下面的网站的根文件夹中的虚拟目录MyVirtualDirectory

However, for paths starting with "~", it will translate the url to the correct, application relative url, e.g., @Url.Content("~/Scripts/SomeScript.js") could translate to /MyVirtualDirectory/Scripts/SomeScript.js, if you deployed your web application to a virtual directory MyVirtualDirectory below the root folder of the web site.

这篇关于为什么要使用@ Url.Content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆