【发布时间】: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>
感谢您的帮助....
鲍勃
【问题讨论】:
-
我也看到了这个帖子:同样的问题,但不确定如何处理 web.confg stackoverflow.com/questions/22545211/… 中的依赖引用
标签: asp.net-web-api cors asp.net-web-api2