asp.net mvc脚本和样式引用

时间:2022-08-26 20:23:30

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()和mvc悔过的<%=Html.ScriptInclude("")%>。

My scripts are in a Scripts folder on the root of the site; my styles are in the usual Content/css/ folder.

我的脚本在站点根目录的scripts文件夹中;我的样式在通常的内容/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. 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?

这是行不通的,因为母版页在共享文件夹中,所以这到底是怎么实现的呢?

2 个解决方案

#1


5  

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

" rel="stylesheet" style ="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") %>

#2


0  

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对你有利的话,也可以把它从你的head标签中去掉。

(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). Works great in every case!

更新:为了澄清,我确实在开发(localhost:1227, root)上,在我的测试服务器(全域、根目录)和生产(不同域,在子目录)中工作。在任何情况下都很有效!

#1


5  

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

" rel="stylesheet" style ="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") %>

#2


0  

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对你有利的话,也可以把它从你的head标签中去掉。

(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). Works great in every case!

更新:为了澄清,我确实在开发(localhost:1227, root)上,在我的测试服务器(全域、根目录)和生产(不同域,在子目录)中工作。在任何情况下都很有效!