1.在Startup.cs中ConfigureServices方法下添加以下代码
// 配置跨域处理,允许所有来源
services.AddCors(options => {
// this defines a CORS policy called "default"
options.AddPolicy("default", policy => {
policy.WithOrigins("*").AllowAnyHeader().AllowAnyMethod();
});
});
2.在Startup.cs中Configure方法下添加以下代码
app.UseCors("default");//跨域
3.在需要跨域的控制器添加
using Microsoft.AspNetCore.Cors;
[EnableCors("default")]//跨越
Stratup.cs总体代码
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;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApi
{
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.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApi", Version = "v1" });
});
// 配置跨域处理,允许所有来源
services.AddCors(options => {
// this defines a CORS policy called "default"
options.AddPolicy("default", policy => {
policy.WithOrigins("*").AllowAnyHeader().AllowAnyMethod();
});
});
}
// 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.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseCors("default");//跨域
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
需要跨域的控制器代码
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Dal;
using WebApi.Models;
namespace WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
[EnableCors("default")]//跨越
public class PatientInformationController : ControllerBase
{
Sqlhelper Sqlhelper = new Sqlhelper();
[HttpGet]
public List<book> Get() {
string sql = "select * from dbo.book";
DataTable a = Sqlhelper.FindAutoInfo(sql);
List<book> book = ToModelList(a);
return book;
}
private List<book> ToModelList(DataTable table)
{
List<book> book = new List<book>();
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
book booke = ToModel(row);
book.Add(booke);
}
return book;
}
private book ToModel(DataRow row)
{
book book = new book();
book.bookid = (int)Sqlhelper.FromDbValue(row["bookid"]);
book.bookname = Sqlhelper.FromDbValue(row["bookname"]).ToString();
book.bookauthor = Sqlhelper.FromDbValue(row["bookauthor"]).ToString();
book.booktype = Sqlhelper.FromDbValue(row["booktype"]).ToString();
book.bookpress = Sqlhelper.FromDbValue(row["bookpress"]).ToString();
book.bookprice = Sqlhelper.FromDbValue(row["bookprice"]).ToString();
book.bookstock = (int)Sqlhelper.FromDbValue(row["bookstock"]);
book.bookstatus = (int)Sqlhelper.FromDbValue(row["bookstatus"]);
return book;
}
}
}