Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...