我有以下服务。需要做一个跨越来源的电话。查看了几篇文章,似乎最简单的方法是将appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll)添加到appBuilder。但是,在执行此操作并检查所有网络流量后,我没有看到任何"访问控制*"标题被注入到回复中。
以下是代码:
// Start OWIN host
using (WebApp.Start(options, (appBuilder) =>
{
//// CORS section
appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// Http configuration
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings =
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
appBuilder.UseWebApi(config);
string contentPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "assets");
var fileServerOptions = new FileServerOptions
{
EnableDirectoryBrowsing = false,
EnableDefaultFiles = true,
DefaultFilesOptions = { DefaultFileNames = { "index.html" } },
FileSystem = new PhysicalFileSystem(contentPath)
};
appBuilder.UseFileServer(fileServerOptions);
}))
{
Console.WriteLine($"The service is running on {baseAddress}");
Console.WriteLine("CTRL-C to exit...");
WaitHandle.WaitOne();
}