【问题标题】:Cannot load assembly 'System.Web.Cors'无法加载程序集“System.Web.Cors”
【发布时间】:2016-07-05 06:50:01
【问题描述】:

我正在另一个端口上调用我的 Web Api 方法,因此我需要启用 CORS

我已经阅读了那里的帖子以获得一些线索:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

我在我的项目中添加了对packages\Microsoft.AspNet.WebApi.Cors.5.2.3\lib\net45 的引用,但是当我运行我的项目时它在线崩溃:

 config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("http://localhost:1360", "*", "*"));

App_Start\WebApiConfig.cs

Chrome 浏览器出现以下错误:

  Could not load file or assembly 'System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. 
 The system cannot find the file specified.

LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MWB-AngularJS\WorkbenchAPI\web.config
LOG: Using host configuration file: C:\Users\robertjm\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/vs/c1b219d5/5d7cc1d5/System.Web.Cors.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/vs/c1b219d5/5d7cc1d5/System.Web.Cors/System.Web.Cors.DLL.
LOG: Attempting download of new URL file:///C:/MWB-AngularJS/WorkbenchAPI/bin/System.Web.Cors.DLL.
LOG: Attempting download of new URL file:///C:/MWB-AngularJS/WorkbenchAPI/bin/System.Web.Cors/System.Web.Cors.DLL.

代码如下:

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();

            // enable Cors - BM:
            //config.EnableCors();
            config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("http://localhost:1360", "*", "*"));
            
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

这是.csproj 文件的一部分,显示了 Cors 参考:

<ItemGroup>
    <Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Web.Entity" />
    <Reference Include="System.Web.ApplicationServices" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Web.Http.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\Microsoft.AspNet.WebApi.Cors.5.2.3\lib\net45\System.Web.Http.Cors.dll</HintPath>
    </Reference>

感谢您的帮助....

鲍勃

【问题讨论】:

标签: asp.net-web-api cors asp.net-web-api2


【解决方案1】:

我也遇到了同样的问题。当我出于某种原因安装 Microsoft.AspNet.WebApi.Cors 时,未安装 Microsoft.AspNet.Cors 包依赖项。手动安装软件包Microsoft.AspNet.Cors 后问题消失了。

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 2014-11-16
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 2016-04-09
    • 2018-05-27
    相关资源
    最近更新 更多