美文网首页.Net Core
.Net Core 添加 Swagger 本地启动可以,发布I

.Net Core 添加 Swagger 本地启动可以,发布I

作者: Rinaloving | 来源:发表于2022-08-29 20:52 被阅读0次
    版本 .net core 2.1
    Program.cs 添加 UseIISIntegration()
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    
    namespace WebApi
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().Run();
            }
    
             public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                #if  DEBUG
                    .UseUrls(AppConfigurtaion.GetSecondSection("Api", "api").ToString())//给程序启动设置url地址
                    .UseKestrel().UseIISIntegration()//使用Kestrel内部服务器
                    .UseStartup<Startup>();
                #elif RELEASE
                   .UseStartup<Startup>().UseKestrel();
                #endif
        }
    }
    
    修改 lauchSettings.json 文件:
    lauchSettings.json.png 修改结果.png
    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localHost:56343",
          "sslPort": 0
        }
      },
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "swagger",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "WebApi": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_URLS": "http://*:5000",
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
          "applicationUrl": "http://localhost:5000"
        }
      }
    }
    
    
    Startup.cs
              services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
                    {
                        Title = "WebApi接口文档",
                        Version = "v1.0.0"
                    });
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, "WebApi.xml");
                    c.IncludeXmlComments(xmlPath,true);
                    //添加对控制器的标签(描述)
                    c.DocumentFilter<SwaggerDocTag>();
                    c.OperationFilter<AuthTokenHeaderParameter>();
                });
    
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");
                    c.RoutePrefix = string.Empty;
                });
    
    勾选生成
    生成.png
    还是不行,然后修改 web.config 文件 :

    添加如下:

       <environmentVariables>
         <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    </environmentVariables>
    

    完整的:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
          </handlers>
          <aspNetCore processPath="dotnet" arguments=".\WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutofProcess">
                    <environmentVariables>
         <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    </environmentVariables>
                </aspNetCore>
        </system.webServer>
      </location>
    </configuration>
    
    IIS 项目发布采用独立方式
    独立.png

    相关文章

      网友评论

        本文标题:.Net Core 添加 Swagger 本地启动可以,发布I

        本文链接:https://www.haomeiwen.com/subject/wdkrnrtx.html