9

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?

Sharun
  • 2,682
  • 5
  • 25
  • 57
jondow
  • 504
  • 4
  • 14
  • Do you mean type="text/javascript"? If so, and you're using HTML5, the type attribute isn't needed. – Netricity Mar 27 '13 at 15:03
  • Hi, I did mean javascript (post edited). Its not HTML5 though. Thanks. – jondow Mar 27 '13 at 15:13
  • Refer “Resource interpreted as script but transferred with MIME type text/plain.” Error - http://stackoverflow.com/questions/3467404/chrome-says-resource-interpreted-as-script-but-transferred-with-mime-type-text – LCJ Nov 08 '13 at 17:42
  • Possible duplicate of [Produce a "type" attribute in the "script" tag when using Scripts.Render in ASP.NET MVC 4](http://stackoverflow.com/questions/13051301/produce-a-type-attribute-in-the-script-tag-when-using-scripts-render-in-asp) – PashaPash Nov 24 '15 at 17:02

4 Answers4

12

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.

Community
  • 1
  • 1
MikeSmithDev
  • 15,236
  • 4
  • 54
  • 85
  • 1
    Thanks, spotted a post with this method in but I thought it was probably not the most effective way. Maybe this is the way to go for now. – jondow Mar 27 '13 at 15:45
  • @jondow What makes it not effective? – MikeSmithDev Mar 27 '13 at 15:50
  • 1
    What is mean is that I expected there to be a built in method such as a overload on the Scripts.Render method or a property that could be set on the ScriptBundle object. – jondow Mar 27 '13 at 16:03
  • It case enable bundling and minification even on debug. How possible disable it on debug? – ali-myousefi May 11 '19 at 11:26
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

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

Matt
  • 21,449
  • 14
  • 100
  • 149
0

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

0

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

β.εηοιτ.βε
  • 16,236
  • 11
  • 41
  • 53
Alaeddine
  • 23
  • 6