我正在尝试在不同的端口上调用我的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>
帮助表示赞赏....
鲍勃
答案 0 :(得分:0)
我遇到了同样的问题。由于某种原因安装了Microsoft.AspNet.WebApi.Cors
后,Microsoft.AspNet.Cors
软件包依赖项尚未安装。手动安装软件包Microsoft.AspNet.Cors
后,该问题消失了。