**
.net core3.1 CORS跨域处理
**
.net core3.1是一个LTS版本,这里我们新建一个core3.1的API项目,目录结构如下:
这里序列化使用了Newtonsoft.Json
UserController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using test.Models;
namespace test.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : Controller
{
[HttpGet]
public ContentResult Index()
{
List<User> users = new List<User>();
users.Add(new User(1, "测试1", 28, "男"));
users.Add(new User(2, "测试2", 25, "女"));
users.Add(new User(3, "测试3", 22, "女"));
List<Order> orders = new List<Order>();
orders.Add(new Order(0001,"电脑"));
orders.Add(new Order(0002, "手机"));
Hashtable result = new Hashtable();
result.Add("user", users);
result.Add("order", orders);
return Content(JsonConvert.SerializeObject(result));
}
}
}
Order.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace test.Models
{
public class Order
{
public int OrderId { get; set; }
public string Product { get; set; }
public Order(int OrderId, string Product)
{
this.OrderId = OrderId;
this.Product = Product;
}
}
}
User.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace test.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public User(int Id, string Name, int Age, string Gender)
{
this.Id = Id;
this.Name = Name;
this.Age = Age;
this.Gender = Gender;
}
}
}
实现跨域需要修改Program.cs和Startup.cs两个文件
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace test
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().
//使用*可以在URL用IP访问,默认使用localhost
UseUrls("http://*:5000;https://*:5001");
});
}
}
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//跨域设置
services.AddCors(options => {
options.AddPolicy("any", builder =>
{
builder.WithOrigins("*");
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
//跨域配置,app.UseCors()需要置于app.UseRouting()和app.UseEndpoints之间
app.UseCors("any");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
这里我们使用Kestrel启动
用PostMan测试https://主机的IP:5001/User显示实现了跨域处理: