1

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 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.