首页 文章

CORS不与Angular 5合作

提问于
浏览
0

我在Angular 5中有ASP.NET Core API和应用程序 . 我在Startup.cs中添加了代码以启用cors并允许所有原点,但是,它仍然给我一个错误

请求的资源上不存在“Access-Control-Allow-Origin”标头 . 因此不允许来源'http:// localhost:4200'访问 . 响应的HTTP状态代码为500 .

以下是我的网络API代码:

public void ConfigureServices(IServiceCollection services)
    {
        WriteToFile("In configure service");
        try
        {
            // Enable Cors
            services.AddCors();
            services.AddMvc();
        }
        catch
        {
            WriteToFile("Not Connected in StartUP");
        }
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        try
        {
            app.UseCors(builder =>
               builder.AllowAnyOrigin()
               .AllowAnyHeader()
               .AllowAnyMethod());

            app.UseMvc();

            WriteToFile("After cors use In configure");

        }
        catch(Exception ex)
        {
            WriteToFile("Error in  configure"+ex.Message);
        }
    }

1 回答

  • 0

    嘿,我觉得你应该放

    [EnableCors(origins: "**http:/...(Here the website you want to enter)**", headers: "*", methods: "*")]
    

    就在Controller的课程之前 .

相关问题