Visual Studio IIS工作正常但在IIS 7中托管脚本时获取错误的URL? [英] Visual Studio IIS works fine but when hosted in IIS 7 scripts gets the wrong URL?
问题描述
我有一个ASP.NET MVC网站,我使用MasterPage中的以下链接
I have a ASP.NET MVC site where I use the following links in the MasterPage
<script type="text/javascript" src="../../../Scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../../Scripts/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" src="../../../Scripts/jquery.cascadingDropDown.js"></script>
<script type="text/javascript" src="../../../Scripts/jquery.maskedinput-1.2.2.js"></script>
从Visual Studio 2010中的内置IIS和主机(IIS7)运行时,此工作正常。但是当它在默认网站\ MySite下的我自己的IIS7中部署时,脚本将得到这样的路径:
This works fine when running from the built in IIS in Visual Studio 2010 and at the host(IIS7). But when deploying it in my own IIS7 under Default Web Site \ MySite the scripts will get a path like this :
http://localhost/Scripts/jquery.cascadingDropDown.js
而不是:
http://localhost/myPage/Scripts/jquery.cascadingDropDown.js
为什么这在VS IIS和我的主机IIS中工作但在我的本地计算机IIS上没有?
Why is this working in VS IIS and my Host IIS but not on my local computer IIS?
网页符文除此之外这个。
The webpage runes fine besides this.
推荐答案
我在ASP.NET MVC中重复了2个绝对基本的规则:
I repeat 2 absolutely fundamental rules in ASP.NET MVC:
-
永远不要像你那样对网址进行硬编码。
在ASP.NET MVC应用程序中处理URL时,始终使用Url助手。
我在数十亿次类似的问题中重复了这么多次,但我仍然看到人们硬编码。
I have been repeating this gazillion of times in gazillion of similar questions and still I see people hardcoding.
所以,如果你使用Razor:
So if you are using Razor:
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.custom.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.cascadingDropDown.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.maskedinput-1.2.2.js")"></script>
如果您使用的是WebForms视图引擎:
And if you are using WebForms view engine:
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-1.4.4.min.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-ui-1.8.11.custom.min.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.cascadingDropDown.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.maskedinput-1.2.2.js") %>"></script>
嘿,如果你使用的是ASP.NET MVC 4(Razor 2.0),那就有一个巧妙的技巧:
And hey, if you are using ASP.NET MVC 4 (Razor 2.0), there's a neat trick:
<script type="text/javascript" src="~/Scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.cascadingDropDown.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.maskedinput-1.2.2.js"></script>
注意〜/
? WebPages 2.0在运行时自动在其上应用 Url.Content
以生成正确的URL。
Notice the ~/
? WebPages 2.0 automatically apply an Url.Content
on it at runtime to produce the correct url.
这篇关于Visual Studio IIS工作正常但在IIS 7中托管脚本时获取错误的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!