.net core webapi 跨域

8 篇文章 0 订阅
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebCoreApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        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.AddMemoryCache();
            services.AddCors(options =>
            {
                //全局起作用
                options.AddDefaultPolicy(
                    builder =>
                    {
                        builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
                    });
                //options.AddDefaultPolicy(
                //builder =>
                //{
                //    builder.WithOrigins("http://example.com", "http://www.contoso.com");
                //});

                /*添加命名的策略和下面的  app.UseCors("AnotherPolicy"); 对应  
                 * 命名的策略可以将不同的策略应用于控制器/页的模型/操作与[EnableCors]属性。 当[EnableCors]特性应用到控制器/页的模型/操作方法和中间件中启用了 CORS,这两个策略的应用。 我们建议不要合并策略。 使用[EnableCors]属性或中间件,不能同时在同一应用中。
                https://docs.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-2.2
                */
                options.AddPolicy("AnotherPolicy",
                    builder =>
                    {
                        builder.WithOrigins("http://www.contoso.com")
                                            .AllowAnyHeader()
                                            .AllowAnyMethod();
                    });

            });
            //services.AddDbContext<TodoContext>(opt => opt.UseMemoryCache())
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            app.UseCors();//这个方法一定要在app.UseMvc之前调用
            //app.UseCors("AnotherPolicy");添加命名的策略   和上面的命名的策略对应

            app.UseMvc(routes =>
            {
                //routes.MapAreaRoute("Cool2", "Cool", "api/cool/{controller}/{action}/{id?}");
                //routes.MapRoute(name: "cool", template: "{area=cool}/{controller=Home}/{action=Index}/{id?}");
                //routes.MapRoute("Blog", "Blog/{*Content}", defaults: new { controller = "Home", action = "Index" });
                routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
            });
            app.UseHttpsRedirection();
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值