zoukankan      html  css  js  c++  java
  • .NET Core API 使用Swagger

    1.项目右键-管理NuGet程序包添加Swashbuckle.AspNetCore

    2.修改Startup.cs中的ConfigureServices方法

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().AddJsonOptions(options =>
                {
                    options.SerializerSettings.ContractResolver = new         DefaultContractResolver();
                });
    
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" });
                });
            }    

    3.修改Startup.cs中的Configure方法

            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseMvc();
    
                app.UseSwagger(c =>
                {
                    c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
                });
    
                app.UseSwaggerUI(c =>
                {
                    c.RoutePrefix = ""; // serve the UI at root
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
                });
            }

    4.删除Properties/launchSettings.json中的 "launchUrl": "api/values",

    5.此时可以使用Swagger

  • 相关阅读:
    浅谈聚类算法(K-means)
    多步法求解微分方程数值解
    本学期微分方程数值解课程总结(matlab代码)
    Stone Game
    Two Sum IV
    Insert into a Binary Search Tree
    Subtree of Another Tree
    Leaf-Similar Trees
    Diameter of Binary Tree
    Counting Bits
  • 原文地址:https://www.cnblogs.com/Adger/p/8591848.html
Copyright © 2011-2022 走看看