|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace CorsServer.WebApi31
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public const string CorsName = "Any";
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
#region Config
|
|
|
|
|
services.Configure<CorsOption>();
|
|
|
|
|
#endregion
|
|
|
|
|
#region CORS
|
|
|
|
|
AddCors_1(services);
|
|
|
|
|
//AddCors_2(services);
|
|
|
|
|
//AddCors_3(services);
|
|
|
|
|
//AddCors_4(services);
|
|
|
|
|
#endregion
|
|
|
|
|
services.AddControllers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F><>·<EFBFBD><C2B7><EFBFBD><EFBFBD>ȫ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ǰ<EFBFBD><C7B0> http://www.custom.com/PathBase/
|
|
|
|
|
//app.UsePathBase("/api/");
|
|
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseCors(CorsName);
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IServiceCollection AddCors_1(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddCors(setup =>
|
|
|
|
|
{
|
|
|
|
|
setup.AddPolicy(CorsName, build =>
|
|
|
|
|
{
|
|
|
|
|
build
|
|
|
|
|
.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.WithExposedHeaders("x-custom-error");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IServiceCollection AddCors_2(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddCors(setup =>
|
|
|
|
|
{
|
|
|
|
|
setup.AddPolicy(CorsName, build =>
|
|
|
|
|
{
|
|
|
|
|
build.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithExposedHeaders("x-custom-error");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IServiceCollection AddCors_3(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddCors(setup =>
|
|
|
|
|
{
|
|
|
|
|
setup.AddPolicy(CorsName, build =>
|
|
|
|
|
{
|
|
|
|
|
build.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithExposedHeaders("x-custom-error");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IServiceCollection AddCors_4(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddCors(setup =>
|
|
|
|
|
{
|
|
|
|
|
setup.AddPolicy(CorsName, build =>
|
|
|
|
|
{
|
|
|
|
|
build.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithExposedHeaders("x-custom-error");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|