You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.2 KiB
C#

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();
}
//¸ù·¾¶£ºÈ«¾Ö·ÃÎÊÇ°ê¡ 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;
}
}
}