开发者

JavaScript url auto-resolution in Asp.Net MVC

开发者 https://www.devze.com 2023-01-05 04:43 出处:网络
I am running Asp.Net MVC 2.0, and I am running into a problem with my JS calls. this is what I have in my :

I am running Asp.Net MVC 2.0, and I am running into a problem with my JS calls.

this is what I have in my :

<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../Scripts/jquery.js"></script>

Which all works fine if I am on the root level. But if I jump up to something like:

Root/Items/Search/term/Page3

the JS doesn't run load - if I look at the source, the MVC is 'smart enough' to re-route the css, but not the Javascript:

<link href="../../../Content/Site.css" rel="stylesheet" type="text/css" /> 
<scri开发者_如何学JAVApt type="text/javascript" src="../../Scripts/jquery.js"></script> 

I must be missing something, there has to be an easy way to do this correctly, any ideas?


To avoid this sort of problems never hardcode links like this. Use helper methods instead:

<link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.js") %>"></script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号