Weary of the .net core 3.1 (a) CORS problem

  Recently due to the end of 2019 9, .net core updated to 3.0, and then look at my previous projects, it did not take long, some things have been abandoned? ? ? Oh, no way, how can you like it, looking at the document look official website feel chant. Today to try out the new service side cross-domain issues.

 

 Accordance with the previous configuration, then the cross-domain TargetFramework to the information given above .net core 3.1.

startup.cs file,

------------------- net core 2.2 project -------------------

public void ConfigureServices(IServiceCollection services){

  services.AddCors(options => {
    options.AddPolicy("any", builder =>
    {

      builder
      .AllowAnyOrigin()
      .AllowAnyMethod()
      .AllowAnyHeader()
      .AllowCredentials();

    });
  });

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env){

  // no cross-domain configuration.

}

------------------- net core 3.1 project -------------------

 

public void ConfigureServices(IServiceCollection services){

    // set the cross-domain
    services.AddCors (Options => {
      options.AddPolicy ( "the any", Builder =>
      {

        builder.WithOrigins("*");
      });
    });

}

 

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){

  // cross-domain configuration. Which between app.UseCors () must be placed app.UseRouting () and app.UseEndpoints, or it will report the above error. Specific reason such as a nice ring to come.

  app.UseRouting();

  app.UseCors("any");

  app.UseEndpoints(...);

}

Guess you like

Origin www.cnblogs.com/inttochar/p/12110196.html