.Net 跨域口诀
上级发政策
services.AddCors(a=>a.AddPolicy("any", configurePolicy=> configurePolicy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
1 // This method gets called by the runtime. Use this method to add services to the container. 2 public void ConfigureServices(IServiceCollection services) 3 { 4 //配置客户端缓存 5 services.AddResponseCaching(); 6 //配置跨域问题 7 services.AddCors(a=>a.AddPolicy("any", configurePolicy=> configurePolicy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); 8 services.AddSession(); 9 services.AddControllersWithViews(); 10 }
政府使用政策
//添加跨域
app.UseCors();
1 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 2 { 3 if (env.IsDevelopment()) 4 { 5 app.UseDeveloperExceptionPage(); 6 } 7 else 8 { 9 app.UseExceptionHandler("/Home/Error"); 10 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 11 app.UseHsts(); 12 } 13 app.UseHttpsRedirection(); 14 app.UseStaticFiles(); 15 //使用客户端缓存 16 app.UseResponseCaching(); 17 //添加跨域 18 app.UseCors(); 19 app.UseRouting(); 20 app.UseSession(); 21 app.UseAuthorization(); 22 23 app.UseEndpoints(endpoints => 24 { 25 endpoints.MapControllerRoute( 26 name: "default", 27 pattern: "{controller=Home}/{action=Index}/{id?}"); 28 }); 29 } 30
百姓受惠利
[EnableCors("Any")]
Cors 跨域职员共享,允许服务器在响应头里面指定Access-Control-Allow-Origin, 浏览器按照响应来操作,
自定义 ActionFilter-response增加头文件Access-Control-Allow-Origin 就可以实现
跨域的根本是因为浏览器的协议不允许,其实只需要在请求返回的时候在
前台跨域:
JQuery.support.cors=true;

浙公网安备 33010602011771号