Reputation: 15807
Hi,
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>
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
Instead of :
http://localhost/myPage/Scripts/jquery.cascadingDropDown.js
Why is this working in VS IIS and my Host IIS but not on my local computer IIS?
The webpage runes fine besides this.
Upvotes: 4
Views: 2547
Reputation: 1038770
I repeat 2 absolutely fundamental rules in ASP.NET MVC:
Never hardcode urls as you did.
Always use Url helpers when dealing with urls in an ASP.NET MVC application.
I have been repeating this gazillion of times in gazillion of similar questions and still I see people hardcoding.
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>
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>
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>
Notice the ~/
? WebPages 2.0 automatically apply an Url.Content
on it at runtime to produce the correct url.
Upvotes: 7