.net mvc 创建的空白mvc使用 @Styles.Render报错 缺少System.Web.Optimization和WebGrease

第一步,引入”Optimization“和”WebGrease“

如果没有这个2个文件的小伙伴,可以另起一个新建项目,创建的时候选择含有模板的项目,创建成功后在bin目录下,就可以找到这个2个文件了,也可以NuGet

第二步,在项目下 App_Start文件夹下增加BundleConfig.cs文件,

代码如下:

using System.Web;
using System.Web.Optimization;

namespace web20200308
{
    public class BundleConfig
    {
        // 有关捆绑的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/js/js_common").Include(
                        "~/Content/js/jquery-1.11.1.min.js",
                        "~/Content/js/bootstrap.min.js")); 

            bundles.Add(new StyleBundle("~/css/css_common").Include(
                      "~/Content/css/bootstrap.min.css",
                      "~/Content/css/master.css"));
        }
    }
}

重点是【using System.Web.Optimization;】

第三步,在项目根目录下的”Views“文件夹下找到 ”web.config“文件找到”namespaces“节点添加

<add namespace="System.Web.Optimization"/>

部分代码

<namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="web20200308" />
</namespaces>

第四步,修改-项目根目录下Global.asax

增加:

 BundleConfig.RegisterBundles(BundleTable.Bundles);

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace web20200308
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

第五步,修改-项目根目录下Web.config,找到”runtime“节点

增加下面代码

      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/>
      </dependentAssembly>

完整代码

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

第六步,可省略

修改-项目根目录下packages.config,找到packages节点

增加

  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.1.3" targetFramework="net46" />
  <package id="WebGrease" version="1.6.0" targetFramework="net46" />

完整代码

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net46" />
  <package id="Microsoft.AspNet.Mvc.zh-Hans" version="5.2.4" targetFramework="net46" />
  <package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net46" />
  <package id="Microsoft.AspNet.Razor.zh-Hans" version="3.2.4" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebPages.zh-Hans" version="3.2.4" targetFramework="net46" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.8" targetFramework="net46" />
  <package id="Microsoft.Net.Compilers" version="2.6.1" targetFramework="net46" developmentDependency="true" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.1.3" targetFramework="net46" />
  <package id="WebGrease" version="1.6.0" targetFramework="net46" />
</packages>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章