欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.net core 跨域

程序员文章站 2024-01-21 19:45:10
...

第一步:修改Startup.cs

一:找到ConfigureServices()方法

网站根目录打开Startup.cs文件,找到ConfigureServices()方法

输入如下代码

 //跨域服务注册
            services.AddCors(m=>m.AddPolicy("any",a=>a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));

二:找到Configure()方法

输入如下代码

 //跨域
app.UseCors();

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
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 swaggertest.Utility;

namespace swaggertest
{
    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)
        {
            SqlHelper.ConStr = Configuration["ConStr"].Trim();

            //跨域服务注册
            services.AddCors(m=>m.AddPolicy("any",a=>a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));

            //Swagger
            services.AddSwaggerGen(m=>{
                m.SwaggerDoc("v1", new OpenApiInfo { Title="swaggerTest",Version="v1" });
            });
            services.AddControllers();
        }

        // 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.UseRouting();

            app.UseAuthorization();

            //Swagger
            app.UseSwagger();
            app.UseSwaggerUI(s=> {
                //下面路径里的v1必须和SwaggerDoc()第一个参数一致
                s.SwaggerEndpoint("/swagger/v1/swagger.json", "swaggerTast");
            });

            //跨域
            app.UseCors();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

第二步:控制器

一、引入using Microsoft.AspNetCore.Cors;

二、控制器Controller 或者方法Action上面添加特性

 [EnableCors("any")]

这里的"any"对应Startup.cs文件ConfigureServices方法里的"any"

//跨域服务注册
            services.AddCors(m=>m.AddPolicy("any",a=>a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));

控制器完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using swaggertest.Models;

namespace swaggertest.Controllers
{
    [EnableCors("any")]
    [Route("api/[controller]/[Action]")]
    //[ApiController]
    public class ProductsController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetProduct()
        {
          List<Products> products= Products.ListAll();
            return new JsonResult(products);
        }
    }
}

 


源码下载

.net core的swagger使用,ADO使用,跨域

https://download.csdn.net/download/cplvfx/12611473

 

相关标签: .net core