为什么要使用@ Url.Content要使、Url、Content

2023-09-03 02:26:37 作者:最熟悉的陌生人

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

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.