【问题标题】:ASP.NET Importing JavaScript FileASP.NET 导入 JavaScript 文件
【发布时间】:2017-06-29 11:10:06
【问题描述】:

我正在尝试将我的自定义 javascript 文件导入我的 asp.net 网络表单。 Visual Studios 在正文中生成样板导入块,如下所示:

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

但是当我尝试添加时:

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

然后运行网站我得到这个错误:

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.

我尝试将此添加到我的BundleConfig.cs

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

但这并没有改变任何事情。知道如何导入我的 js 文件吗?

【问题讨论】:

    标签: javascript c# asp.net visual-studio webforms


    【解决方案1】:

    1.只要使用路径,就可以了,不需要任何额外的代码。

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

    2.如果你想在asp scriptreference中加载Name。

    BundleConfig.cs

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

    网站管理员

    <asp:ScriptReference Name="site" />
    

    3.从bundle中引用脚本的其他方式。

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

    将其包含在站点主文件中

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

    4.直接引用脚本。

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

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 2021-03-28
      • 2021-02-26
      • 1970-01-01
      • 2021-03-19
      • 2020-09-04
      • 2020-01-30
      • 1970-01-01
      • 2019-11-12
      相关资源
      最近更新 更多