1、nuget安装microsoft.aspnetcore.cors
2、program.cs文件中
using System.Reflection; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); //设置跨域 builder.Services.AddCors(options => { options.AddPolicy("Cors", builder => { builder.WithOrigins("http://localhost/5173", "http://localhost/5174") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); var app = builder.Build(); app.UseCors("Cors");//启用跨域
3、在接口文件中demo.cs
using Microsoft.AspNetCore.Cors; /// <summary> /// 信息读写接口 /// </summary> [ApiController] [Route("[controller]")] [EnableCors("myCors")] public class RData : ControllerBase { [HttpPost] public string Post() { return ""; } }
标签:webapi,跨域,using,builder,Cors,cors,net6,app From: https://www.cnblogs.com/yingshiyu/p/17970691