我在文件startup.cs
中添加了以下代码。
方法:ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<Models.StaticDataContext>(options => options.UseSqlServer(connection));
services.AddMvc(o => {});
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddCors(o=> { o.AddPolicy("AllowSpecificOrigin", b => b.WithOrigins("*")); });
// Tried it without any parameter
// services.AddCors();
}
方法:配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader());
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
}
但是它不会返回Access-Control-*
的标题?
OPTIONS http://localhost:5001/api/Deal2 HTTP/1.1
Host: localhost:5001
Connection: keep-alive
Access-Control-Request-Method: PUT
Origin: http://localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:8082/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
原始反应来自小提琴手。
HTTP/1.1 204 No Content
Date: Sun, 27 Nov 2016 23:32:31 GMT
Server: Kestrel
以下应该有一些标题?
Access-Control-Allow-Origin: YOUR_DOMAIN
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: YOUR_CUSTOM_HEADERS
答案 0 :(得分:0)