[英]How do I add type=“text/javascript” to a script tag when using System.Web.Optimization
I have the following我有以下
bundles.Add(new ScriptBundle("~/bundles/scripts/common").Include(
"~/Scripts/jquery.validationEngine.js",
"~/Scripts/common.js"));
Which generates其中产生
<script src="/bundles/scripts/common?v=9O0Yi3fV_GWpGyJQ_QYURiOYy6SEmxUQtkUVN4GXo2U1"></script>
When rendered with渲染时
<asp:PlaceHolder ID="PlaceHolderJs" runat="server">
<%: Scripts.Render("~/bundles/scripts/common") %>
</asp:PlaceHolder>
Which is not valid HTML as its missing type="text/javascript".这不是有效的 HTML,因为它缺少 type="text/javascript"。 How do I make the BundleCollection output this element in the script tag?
如何让 BundleCollection 在脚本标签中输出这个元素?
One way is to change how you render your scripts:一种方法是更改呈现脚本的方式:
From:从:
@Scripts.Render("~/bundles/scripts/common")
To:到:
<script src="@BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts/common")" type="text/javascript"></script>
Or depending on how you are implementing bundling, you may need:或者,根据您实施捆绑的方式,您可能需要:
<script src="@Microsoft.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts/common")" type="text/javascript"></script>
Or for web forms:或者对于网络表单:
<script src="<%= Microsoft.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/scripts/common")%>" type="text/javascript"></script>
Although if there is a way to do it using @Script.Render I'd like to see it.虽然如果有办法使用 @Script.Render 来做到这一点,我想看看它。
UPDATE: in response to your comments, as specified in this SO answer , in the pre-release version of System.Web.Optimization
, there is an option called RenderFormat
that will let you do this as well... but I think the stuff above is easier to read for this particular case.更新:为了回应你的评论,正如在这个 SO answer 中所指定的那样,在
System.Web.Optimization
的预发布版本中,有一个名为RenderFormat
的选项也可以让你这样做......但我认为这些东西对于这种特殊情况,上面更容易阅读。
I believe the answer marked is not correct for type="text/css"
, it is correct for JavaScript (maybe the question title is misleading?).我相信标记的答案对于
type="text/css"
不正确,对于 JavaScript 是正确的(也许问题标题有误导性?)。 For CSS files you should use对于 CSS 文件,您应该使用
@Styles.Render("~/bundles/styles/common")
and not @Scripts.Render(...)
.而不是
@Scripts.Render(...)
。 The reason is that minification is done, and only @Styles.Render(...)
knows about Cascading Stylesheet syntax, @Scripts.Render(...)
is specialized for JavaScript minification.原因是缩小已经完成,只有
@Styles.Render(...)
知道层叠样式表语法, @Scripts.Render(...)
专门用于 JavaScript 缩小。
I have resolved it like that我已经这样解决了
> bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
> "~/Scripts/node_modules/jquery/dist/jquery.min.js"));
i think you need to replace ~/bundles/scripts/common with ~/bundles/jquery我认为你需要用 ~/bundles/jquery 替换 ~/bundles/scripts/common
I have resolved it like that我已经这样解决了
bundles.Add(
new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/node_modules/jquery/dist/jquery.min.js"
)
);
I think you need to replace ~/bundles/scripts/common
with ~/bundles/jquery
我认为你需要用
~/bundles/jquery
替换~/bundles/scripts/common
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.