zoukankan      html  css  js  c++  java
  • 基于asp.net core webapi的衣服商城手机版--使用swaggerui

    swagger使用并跨域

    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 Xwy.WebApiDemo.Models;
    
    namespace Xwy.WebApiDemo
    {
        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.AddDbContext<ClothersMallDbContext>(m=> { 
                    
                });
                services.AddSwaggerGen(m => 
                {
                    m.SwaggerDoc("v1",new OpenApiInfo() { Title="swaggertest",Version="v1"});
                });
                services.AddCors(m => m.AddPolicy("any", a => a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
                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();
    
                app.UseSwagger();
                app.UseCors();
                app.UseSwaggerUI(m=> {
                    m.SwaggerEndpoint("/swagger/v1/swagger.json","swaggertest");
    
                });
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    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;
    
    namespace Xwy.WebApiDemo.Controllers
    {
        [EnableCors("any")]
        [Route("[controller]/[action]")]
        [ApiController]
        public class ProductsController : ControllerBase
        {
            [HttpGet]
            public IActionResult GetAllProducts()
            {
    
                return new JsonResult("2233");
            }
        }
    }
  • 相关阅读:
    《校园封神榜》第二阶段个人工作总结——第五天
    寻找水王2——寻找三个小水王
    站立会议04(第二次冲刺)
    站立会议03(第二次冲刺)
    站立会议02(第二次冲刺)
    站立会议01(第二次冲刺)
    测试计划
    评价cnblogs.com的用户体验
    第一次冲刺各组评价的回复
    第一次冲刺对各组的评价
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13088886.html
Copyright © 2011-2022 走看看