gpt4 book ai didi

asp.net-core - ASP.NET Core Swagger 无法在 Chrome 中使用 CORS 错误

转载 作者:行者123 更新时间:2023-12-03 20:17:31 29 4
gpt4 key购买 nike

我一直在关注 ASP.NET Web API Help Pages using Swagger 的帮助,但在 Chrome 中收到以下错误:

  • 无法从服务器读取。它可能没有适当的访问控制来源设置。

  • 它在 IE10 中工作,但它似乎没有进行更改。

    我发现以下条目 Can't read swagger JSON from http://localhost:9000/api-docs/ 不幸的是,它指的是现在在 gulp 下工作时在 grunt 下更改它。

    我还尝试更改 ASP.NET 核心中的 CORS 设置:
     public void ConfigureServices(IServiceCollection services)
    {
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();

    // Inject an implementation of ISwaggerProvider with defaulted settings applied
    services.AddSwaggerGen();


    services.ConfigureSwaggerGen(options =>
    {
    options.SingleApiVersion(new Info
    {
    Version = "v1",
    Title = "Status API",
    Description = "A simple example ASP.NET Core Web API!",
    TermsOfService = "None",
    Contact = new Contact { Name = "A Persone", Email = "some-support@some-company.com", Url = "http://www.some-company.com/" },
    License = new License { Name = "Use under LICX", Url = "http://url.com" }
    });
    });

    services.AddCors(options =>
    {
    options.AddPolicy("AnyOrigin", builder =>
    {
    builder
    .AllowAnyOrigin()
    .AllowAnyMethod();
    });
    });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseApplicationInsightsRequestTelemetry();

    app.UseApplicationInsightsExceptionTelemetry();

    app.UseMvc();

    // Enable middleware to serve generated Swagger as a JSON endpoint
    app.UseSwagger();

    // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
    app.UseSwaggerUi();

    app.UseCors("AnyOrigin");

    }

    不幸的是,这没有任何区别。

    关于如何通过更改 gulp 设置或 .NET 更改来解决它的建议将非常受欢迎

    最佳答案

    通常这应该可以解决问题,您只需要它具有相同的策略名称。如果您想将其应用于所有请求/路由,我认为没有必要添加过滤器。

    // ConfigureServices
    services.AddCors(options =>
    {
    options.AddPolicy("AnyOrigin", builder =>
    {
    builder
    .AllowAnyOrigin()
    .AllowAnyMethod();
    });
    });

    // Configure
    app.UseCors("AnyOrigin");
    // Register other middleware here, like UseMvc or UseStaticFiles, depending on if you
    // want StaticFiles to be affected by cors or not you put it before or after the UseCors call

    更新

    就像上面的评论中提到的那样,中间件以它们注册的方式执行。该请求在 Cors 中间件收到之前到达您的 API Controller 。

    您的 Configure 方法必须如下所示:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseApplicationInsightsRequestTelemetry();

    app.UseApplicationInsightsExceptionTelemetry();

    // it must be placed before UseMvc and before or after
    // UseStaticFiles, depending on if you want the static files to be
    // Cors enabled or not
    app.UseCors("AnyOrigin");

    app.UseMvc();

    // Enable middleware to serve generated Swagger as a JSON endpoint
    app.UseSwagger();

    // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
    app.UseSwaggerUi();
    }

    关于asp.net-core - ASP.NET Core Swagger 无法在 Chrome 中使用 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39211778/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com