在ASP.NET MVC 中,Master Page 在其 Head 节中可以引用 CSS 以及 JS 文件。但是不知为什么,CSS 文件可以自动解析为客户端可用的引用路径,而 JS 文件则原封不动的使用设计时设定的路径,即使使用 "~/" 也不行。
怎么办呢?我想到了用 Helper 方法: Url.Content:
Code
<head runat="server">
<script type="text/javascript" src="<%= Url.Content("~/Content/jquery-1.2.6.min.js") %>"></script>
</head>
结果报错:
Code
Server Error in '/' Application.
--------------------------------------------------------------------------------
The Controls collection cannot be modified because the control contains code blocks (i.e. <% %>).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% %>).
没办法了,Google一把,关键词就是The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
终于找到解决方法,特意共享给大家,原文:The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
虽然其场景并非 MVC,但值得借鉴。
我的修改如下
Code
<head runat="server">
<asp:ContentPlaceHolder ID="usedOnlyForMaster" runat="server">
<script type="text/javascript" src="<%= Url.Content("~/Content/jquery-1.2.6.min.js") %>"></script>
</asp:ContentPlaceHolder>
</head>
OK!问题解决!