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");
            }
        }
    }
  • 相关阅读:
    一些我遇到前端方面的问题和解决方法
    Effective Objective-C 2.0学习记录(二)
    Effective Objective-C 2.0学习(一)
    加快Xcode运行速度
    JPA CriteriaBuilder的简单使用
    日志切分
    iOS并发,串行,异步,同步
    服务重启脚本
    简述http/https加密和认证方式
    nohup的使用
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13088886.html
Copyright © 2011-2022 走看看