使用System.Web.Optimization时,如何将type =“text / javascript”添加到脚本标记中

时间:2022-11-07 18:58:33

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". How do I make the BundleCollection output this element in the script tag?

哪个不是有效的HTML,因为它缺少类型=“text / javascript”。如何使BundleCollection在脚本标记中输出此元素?

2 个解决方案

#1


10  

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答案中所述,在System.Web.Optimization的预发布版本中,有一个名为RenderFormat的选项,它也可以让你这样做...但我认为这些东西对于这种特殊情况,上面的内容更容易理解。

#2


0  

I believe the answer marked is not correct for type="text/css", it is correct for JavaScript (maybe the question title is misleading?). For CSS files you should use

我认为标记的答案对于type =“text / css”是不正确的,它对JavaScript是正确的(可能问题标题是误导性的?)。对于CSS文件,您应该使用

@Styles.Render("~/bundles/styles/common")

and not @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.

而不是@ Scripts.Render(...)。原因是缩小已完成,只有@ Styles.Render(...)知道级联样式表语法,@ Scripts.Render(...)专门用于JavaScript缩小。

#1


10  

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答案中所述,在System.Web.Optimization的预发布版本中,有一个名为RenderFormat的选项,它也可以让你这样做...但我认为这些东西对于这种特殊情况,上面的内容更容易理解。

#2


0  

I believe the answer marked is not correct for type="text/css", it is correct for JavaScript (maybe the question title is misleading?). For CSS files you should use

我认为标记的答案对于type =“text / css”是不正确的,它对JavaScript是正确的(可能问题标题是误导性的?)。对于CSS文件,您应该使用

@Styles.Render("~/bundles/styles/common")

and not @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.

而不是@ Scripts.Render(...)。原因是缩小已完成,只有@ Styles.Render(...)知道级联样式表语法,@ Scripts.Render(...)专门用于JavaScript缩小。