Rob*_*ker 7 asp.net asp.net-mvc
更新 ASP.NET MVC的发布位中不存在此问题
我有一个ASP.NET MVC RC应用程序在http:// localhost:9002 /运行的调试器下运行正常 但是当我将它发布到http:// localhost/Zot /时,对Url.Content的调用返回的值不正确.
我有脚本标签
<script src="<%= Url.Content("~/Scripts/util.js") %>" ...
Run Code Online (Sandbox Code Playgroud)
在发布的网站中,这会产生:
<script src="Zot/Scripts/util.js" ...
Run Code Online (Sandbox Code Playgroud)
代替
<script src="/Zot/Scripts/util.js" ...
Run Code Online (Sandbox Code Playgroud)
要么
<script src="Scripts/util.js" ...
Run Code Online (Sandbox Code Playgroud)
我有样式表标签,如:
<link href="~/Content/Site.css" runat="server" ...
Run Code Online (Sandbox Code Playgroud)
这产生了正确的事情:
<link href="Content/Site.css" ...
Run Code Online (Sandbox Code Playgroud)
关于为什么Url.Content失败的任何建议.我显然不能runat="server"在<script>标签上放一个.
我倾向于使用Rob Conery的脚本注册助手:
public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, string scriptLib) {
//get the directory where the scripts are
string scriptRoot = VirtualPathUtility.ToAbsolute("~/Scripts");
string scriptFormat="<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";
return string.Format(scriptFormat,scriptRoot,scriptLib);
}
Run Code Online (Sandbox Code Playgroud)
用法:
<%= Html.RegisterJS("myscriptFile.js") %>
Run Code Online (Sandbox Code Playgroud)
正如您在示例中所看到的,这使用VirtualPathUtility来解析Scripts目录的URL.这也应该有助于绕过标签汤的问题.
| 归档时间: |
|
| 查看次数: |
9539 次 |
| 最近记录: |