简体   繁体   English

ASP.NET Core API 启用 Cors 不起作用

[英]ASP.NET Core API Enabling Cors not working

First time trying to make an api with asp.net core, I am using ASP.NET Core 3.1.第一次尝试使用 ASP.NET Core 制作 api,我使用的是 ASP.NET Core 3.1。 I run into this console error when I try to send a GET request:当我尝试发送 GET 请求时遇到此控制台错误:

Access to XMLHttpRequest at 'https://localhost:5001/api/items/1' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 

I have tried a lot of solutions on this site and from others and they do not seem to be working for me.我在这个网站和其他网站上尝试了很多解决方案,但它们似乎对我不起作用。 Here is what I have for my Javascript side:这是我的 Javascript 方面的内容:

const sendRequest = async function(url) {
    try {
        let response = await axios.get(url);
        return response;
    } catch (error) {
        console.error(error);
        return [];
    }
};

Here is what I have in Startup.cs这是我在 Startup.cs 中的内容

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("AllowMyOrigin",
                builder => builder.WithOrigins(
                    "http://localhost:8080/")
                    .WithMethods("POST", "GET", "PUT")
                    .WithHeaders("*")
                    );
            });
            services.AddDbContext<DBContext>(opt =>
            opt.UseSqlServer(Configuration.GetConnectionString("DatabaseName")));
            services.AddControllers();
            
            
        }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            
            //app.UseAuthentication();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseCors("AllowMyOrigin");
            //app.UseAuthorization();
            //app.UseAuthentication();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

If this is too little info I will try to clarify, but like I said this is my first time doing something like this that deals with the cors policy.如果这是太少的信息,我会尝试澄清,但就像我说的,这是我第一次做这样的事情来处理 cors 政策。

builder.WithOrigins("http://localhost:8080/") builder.WithOrigins("http://localhost:8080/")

Please note that the specified URL must not contain a trailing slash ( / ).请注意,指定的 URL 不得包含尾部斜杠 ( / )。 Please modify the code like below, then check if it works well.请修改如下代码,然后检查它是否工作正常。

services.AddCors(options =>
{
    options.AddPolicy("AllowMyOrigin",
        builder => builder.WithOrigins(
            "http://localhost:8080")
        .WithMethods("POST", "GET", "PUT")
        .AllowAnyHeader()
        );
});

Please try adding this inside请尝试在里面添加这个

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM