简体   繁体   English

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

[英]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.

相关问题 无法添加system.web.optimization - cannot add system.web.optimization 使用 System.Web.Optimization 捆绑,我如何访问构建捆绑的结果? - Using System.Web.Optimization Bundling, How Can I access The Result of a Built Bundle? 如何设置System.Web.Optimization捆绑程序以正确转换ISO Unicode - How can I set the System.Web.Optimization bundler to correctly transform ISO unicode 我可以将System.Web.Optimization与AMD加载器(如requireJS)一起使用吗? - Can I use System.Web.Optimization toghether with an AMD loader like requireJS? 使用&#39;System.Web.Optimization&#39;压缩由asp.net捆绑创建的包? - Compression of bundles created by asp.net bundling using 'System.Web.Optimization'? 在ASP.NET MVC中使用System.Web.Optimization和WebGrease实现自动矿化而无需捆绑 - Automate minfication using System.Web.Optimization & WebGrease in asp.net mvc without bundling System.Web.Optimization是.Net Framework 4.5的一部分吗? - Is System.Web.Optimization part of .Net Framework 4.5? 获取对System.Web.Optimization中使用的URL的访问 - Get access to the URL's being used in System.Web.Optimization 哪个NuGet软件包包含System.Web.Optimization? - Which NuGet package contains System.Web.Optimization? MVC 3 System.Web.Optimization捆绑单个文件 - MVC 3 System.Web.Optimization Bundles single file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM