我正在不同的端口上调用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
的引用,但是当我运行我的项目时,它崩溃了:
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>
感谢你的帮助..。
鲍勃
发布于 2020-01-16 10:44:42
我也遇到过同样的问题。当我出于某种原因安装了Microsoft.AspNet.WebApi.Cors
时,没有安装Microsoft.AspNet.Cors
包依赖项。手动安装Microsoft.AspNet.Cors
包后,该问题消失了。
https://stackoverflow.com/questions/36085685
复制相似问题