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");
            }
        }
    }
  • 相关阅读:
    【六道无鱼】ExifTool编辑修改图片GPS
    【Elastic】Filebeat+ELK日志收集分析方案
    【Cesium】鹰眼地图功能
    【Cesium】3dtiles模型单体化
    【PHP】Version 7.2.13 报错 Fatal error: Call to undefined function curl_init()解决方案
    【ODM】win10 安装 webODM
    【数据库】mysql 删除多个关联的表
    【ElasticSearch】win10 安装elasticSearch 6.6.1
    【Cesium】视域分析 基于3dtiles做的视域分析
    缕清思路,继续前行
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13088886.html
Copyright © 2011-2022 走看看