Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET核心网页忽略Startup.cs中的CORS策略_C#_Visual Studio_Asp.net Core_Cors_Asp.net Core 2.1 - Fatal编程技术网

C# ASP.NET核心网页忽略Startup.cs中的CORS策略

C# ASP.NET核心网页忽略Startup.cs中的CORS策略,c#,visual-studio,asp.net-core,cors,asp.net-core-2.1,C#,Visual Studio,Asp.net Core,Cors,Asp.net Core 2.1,这不是以前问题的重复,因为他们的解决方案是添加CORS策略。我在这里的问题是,为什么CORS政策在这种情况下被忽视,尽管它存在 我正在运行一个基本的ASP.NETCore2.1网页,它将与运行在localhost:5082上的docker容器进行通信。是的,.NETCore2.1有点老了,但由于部门类型的原因,它目前对我们来说是一个必要的缺点 因此,当尝试通过VisualStudio调试器运行该网页项目时,由于CORS策略,当尝试访问该容器时,页面上的AJAX请求自然会失败。所以…我加入了Sta

这不是以前问题的重复,因为他们的解决方案是添加CORS策略。我在这里的问题是,为什么CORS政策在这种情况下被忽视,尽管它存在

我正在运行一个基本的ASP.NETCore2.1网页,它将与运行在localhost:5082上的docker容器进行通信。是的,.NETCore2.1有点老了,但由于部门类型的原因,它目前对我们来说是一个必要的缺点

因此,当尝试通过VisualStudio调试器运行该网页项目时,由于CORS策略,当尝试访问该容器时,页面上的AJAX请求自然会失败。所以…我加入了Startup.cs,加入了一个非常自由的CORS政策,出于某种原因,它被忽略了

我试过几篇类似的帖子和答案,它们都做了非常相似的事情,但这是我的Startup.cs可能看起来像的一个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace TheNamespace
{
    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.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                    builder =>
                    {
                        builder
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials();
                    });
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseCors("AllowAll");
            app.UseMvc();
        }
    }
}
怎么了

这是失败的JavaScript url是字符串值:

var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', url, false);
//xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlHttp.send(null);
return xmlHttp.responseText;
编辑:

这不是一个MVC项目或任何花哨的东西。创建项目时,我只是选择了Web应用程序的选项。所以现在,它没有控制器,只有页面

编辑:

根据评论,这基本上是我的项目结构:

Pages:
    Shared:
        _CookieConsentPartial.cshtml
        _Layout.cshtml
        _ValidationScriptsPartial.cshtml
    _ViewImports.cshtml
    _ViewStart.cshtml
    Index.cshtml
Program.cs
Startup.cs

就URL而言,它是http://localhost:5082/api/buildcactus.这对我来说很有效。生成器中的CorsPolicyBuilder配置策略。现在,您可以在控制器中使用它:

[EnableCors("AllowAll")]
public class MyController
{

}

app.useCorsAllow单独可能不适用于API端点。尝试在控制器上显式添加[EnableCorsAllowAll]。

尝试移动应用程序。UseCorsAllowAll;在app.UseStaticFiles上方;callI知道你说你正在使用2.1,但是在2.2中有一个改变,这意味着允许CORS的一切不再有效:我认为如果你展示你的实际项目结构,以及你调用的实际URL,会有所帮助。啊。。。当您的JS试图命中您的端点时,您得到的HTTP状态代码是什么?请扩展我之前的评论:在ASP.NET Core 2.1中,当请求管道中发生异常且未显式处理时,异常处理中间件将清除所有HTTP头,包括CORS头。这在2.2中得到了修正:
[EnableCors("AllowAll")]
public class MyController
{

}