首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法加载程序集“System.Web.Cors”

无法加载程序集“System.Web.Cors”
EN

Stack Overflow用户
提问于 2016-03-18 21:28:39
回答 1查看 5.1K关注 0票数 2

我正在不同的端口上调用Web方法,所以我需要启用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的引用,但是当我运行我的项目时,它崩溃了:

代码语言:javascript
运行
复制
 config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("http://localhost:1360", "*", "*"));

App_Start\WebApiConfig.cs

在Chrome浏览器中出现以下错误:

代码语言:javascript
运行
复制
  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.

代码语言:javascript
运行
复制
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.

代码如下:

代码语言:javascript
运行
复制
 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参考:

代码语言:javascript
运行
复制
<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>

感谢你的帮助..。

鲍勃

EN

回答 1

Stack Overflow用户

发布于 2020-01-16 18:44:42

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36085685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档