欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.net core webapi 跨域

程序员文章站 2024-01-21 19:05:46
...
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();
        }
    }
}

 

上一篇: 定义 php 变量定义方法

下一篇: