2017-02-11 74 views
0

我正在尝试将我的自定义JavaScript文件导入到我的asp.net webforms中。视觉工作室产生的,看起来像这样身体内的样板进口块:ASP.NET导入JavaScript文件

<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文件?

回答

3

1.使用路径,它会工作,不需要任何额外的代码。

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

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

BundleConfig.cs

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

SITEMASTER从束指剧本

<asp:ScriptReference Name="site" /> 

3.其他方式。

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

包括在网站母

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

4.Directly参考脚本。

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