public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
readonly string MyAllowSpecificOrigins = “_myAllowSpecificOrigins”;
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//这里跨域
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder => builder.WithOrigins(“http://192.168.1.167:8074”, “http://192.168.1.167:8095”)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
services.AddMvc().AddWebApiConventions();
services.AddControllers();
services.AddStackExchangeRedisCache(opsw =>
{
opsw.Configuration = "127.0.0.1:6379";//地址
opsw.InstanceName = "Lw_";//标识
});
services.AddDbContext<fzcontext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("gww")));
services.AddSwaggerGen(c=>{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
// app.UseMiddleware<CorsMiddleware>();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
app.UseRouting();
//一定要在中间
app.UseCors(MyAllowSpecificOrigins);
app.UseEndpoints(endpoints =>
{