简体   繁体   English

asp.net mvc脚本和样式引用

[英]asp.net mvc script and style references

I'm trying to include script and style references that will not break on deployment, however I can not even get the references to work locally. 我正在尝试包含在部署时不会中断的脚本和样式引用,但是我甚至无法使引用在本地工作。 I have tried using Url.Content() and MVCContrib's <%=Html.ScriptInclude("")%> . 我尝试过使用Url.Content()和MVCContrib的<%=Html.ScriptInclude("")%>

My scripts are in a Scripts folder on the root of the site; 我的脚本位于站点根目录下的Scripts文件夹中; my styles are in the usual Content/css/ folder. 我的样式在通常的Content / css /文件夹中。

The scripts render like this: 脚本渲染如下:

<script type="text/javascript" src="/Scripts/MicrosoftAjax.debug.js" ></script>

This will not work in a view page in the Views folder. 这不适用于Views文件夹中的视图页面。 What am I doing wrong and what is the best way to handle this? 我做错了什么,处理这个问题的最佳方法是什么?

I would have thought Url.Content() would at least work for styles but used in my master page, the link rendered 我原以为Url.Content()至少适用于样式,但在我的母版页中使用,链接呈现

<link href="/Content/css/Site.css rel="stylesheet" type="text/css" /> 

This does not work, because the Master Page is in a Shared folder, so what is really the way forward with this? 这不起作用,因为母版页位于共享文件夹中,那么真正的前进方向是什么呢?

<link href="<%= ResolveUrl("~/Content/css/Site.css")%>" rel="stylesheet" type="text/css" / > <link href="<%= ResolveUrl("~/Content/css/Site.css")%>" rel="stylesheet" type="text/css" / >

works for your style sheet. 适用于您的样式表。 but if you are on MVC2 and you have the files in the script dir then you can use the new helper: 但如果您使用的是MVC2并且脚本目录中有文件,则可以使用新的帮助程序:

<%=Html.Script("scriptfile.js") %>

this is better practice as you can also specify a file for release and debug mode: 这是更好的做法,因为您还可以为发布和调试模式指定文件:

<%=Html.Script("scriptfile-min.js", "scriptfile.js") %>

I do it like this: 我是这样做的:

<link href="<%= ResolveUrl("~/Content/css/Site.css")%>" rel="stylesheet" type="text/css" />

Also, take the runat=server out of your head tag if you don't want it trying to do you favors. 另外,如果您不希望它尝试对您有利,请将runat = server从头标记中删除。

(Edit - fixed a misplaced quote) (编辑 - 修正错误的报价)

Update: To clarify, I do in fact have this working in development (localhost:1227, root), on my test server (full domain, root) and in production (different domain, in a subdirectory). 更新:为了澄清,我确实在我的测试服务器(完整域,root)和生产(不同域,子目录)中开发(localhost:1227,root)。 Works great in every case! 在每种情况下都很棒!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM