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.
81 lines
2.2 KiB
C#
81 lines
2.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;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace CorsServer.WebApi31
|
|
{
|
|
public class StartupDefaultPolicy
|
|
{
|
|
public StartupDefaultPolicy(IConfiguration configuration, IHostEnvironment hostingEnvironment, IWebHostEnvironment webHostEnvironment)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
//config
|
|
services.Configure<CorsOption>(Configuration.GetSection("CORS"));
|
|
|
|
//Cors
|
|
AddDefaultCors(services);
|
|
|
|
services.AddControllers();
|
|
}
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsSnapshot<CorsOption> corsOtionsSnapshot)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
//根路径:全局访问前辍 http://www.custom.com/PathBase/
|
|
//app.UsePathBase("/api/");
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseCors();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置默认策略Cors
|
|
/// </summary>
|
|
private IServiceCollection AddDefaultCors(IServiceCollection services)
|
|
{
|
|
services.AddCors(setupCors =>
|
|
{
|
|
setupCors.AddDefaultPolicy(build =>
|
|
{
|
|
build
|
|
.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.SetPreflightMaxAge(TimeSpan.FromMinutes(10))
|
|
;
|
|
});
|
|
});
|
|
|
|
return services;
|
|
}
|
|
}
|
|
}
|