Skip to content Skip to sidebar Skip to footer

Visual Studio Iis Works Fine But When Hosted In Iis 7 Scripts Gets The Wrong Url?

Hi, I have a ASP.NET MVC site where I use the following links in the MasterPage

Solution 1:

I repeat 2 absolutely fundamental rules in ASP.NET MVC:

  1. Never hardcode urls as you did.

  2. 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:

<scripttype="text/javascript"src="~/Scripts/jquery-1.4.4.min.js"></script><scripttype="text/javascript"src="~/Scripts/jquery-ui-1.8.11.custom.min.js"></script><scripttype="text/javascript"src="~/Scripts/jquery.cascadingDropDown.js"></script><scripttype="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.

Post a Comment for "Visual Studio Iis Works Fine But When Hosted In Iis 7 Scripts Gets The Wrong Url?"