【问题标题】:My Vue APp doesn't work with ASP.NET because of Cors Error由于 Cors 错误,我的 Vue APP 无法与 ASP.NET 一起使用
【发布时间】:2022-12-03 08:52:06
【问题描述】:

我有一个 VUE 3 应用程序和一个在 IIS 上运行的 Cors 应用程序。 另外,如果我将以下代码放入 ASP.NET 中:

app.UseCors(builder => builder.WithOrigins("*")
                        .AllowAnyMethod()
                        .AllowAnyHeader());

它不起作用,我收到 CORS 错误。 我希望有人能帮我解决这个问题。

【问题讨论】:

    标签: c# asp.net vue.js cors


    【解决方案1】:

    您似乎正在尝试使用 ASP.NET 为您的 VUE 3 应用程序启用 CORS。要在 ASP.NET 中启用 CORS,您需要使用 Microsoft.AspNetCore.Cors NuGet 包。

    以下是如何在 ASP.NET 中启用 CORS 的示例:

    using Microsoft.AspNetCore.Cors;
    
    //...
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll",
                builder =>
                {
                    builder.AllowAnyOrigin()
                           .AllowAnyMethod()
                           .AllowAnyHeader();
                });
        });
    }
    
    public void Configure(IApplicationBuilder app)
    {
        app.UseCors("AllowAll");
    
        //...
    }
    

    将此代码添加到 ASP.NET 应用程序后,您应该能够从 VUE 3 应用程序发出跨源请求而不会出现 CORS 错误。

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 2021-02-07
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2019-01-22
      相关资源
      最近更新 更多