简体   繁体   中英

ASP.NET Importing JavaScript File

I am trying to import my custom javascript file into my asp.net webforms. Visual Studios generated boilerplate import block inside the body that looks like this:

<asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="respond" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>

But when I try to add:

<asp:ScriptReference Name="Site.js" Path="Scripts/Site.js"/>

And run the site I get this error:

The assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not contain a Web resource that has the name 'Site.js'. Make sure that the resource name is spelled correctly.

I tried to add this to my BundleConfig.cs :

bundles.Add(new ScriptBundle("~/bundles/Site").Include(
                            "~/Scripts/Site.js"));

But that didn't change anything. Any idea how to import my js file?

1.Just use path, it will work and no need any additional code.

<asp:ScriptReference Path="Scripts/Site.js"/>

2.If you want to load with Name in asp scriptreference.

BundleConfig.cs

ScriptManager.ScriptResourceMapping.AddDefinition(
                "site",
                new ScriptResourceDefinition
                {
                    Path = "~/Scripts/Site.js",
                });

SiteMaster

<asp:ScriptReference Name="site" />

3.Other way of referring script from bundle.

bundles.Add(new ScriptBundle("~/bundles/Site").Include(
                            "~/Scripts/Site.js"));

Include it in site master

 <asp:PlaceHolder runat="server">
         <%: Scripts.Render("~/bundles/Site") %>
    </asp:PlaceHolder>

4.Directly refer the scripts.

<script src="Scripts/Site.js" type="text/javascript"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM