好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

如何在Asp.Net Core Mvc项目中起用api及swagger

如何在Asp.Net Core Mvc项目中起用api及swagger

起用API的方法

1、program.cs文件参考如下配置:

namespace HdhCmsWebCoreMvc

{

    public class Program

    {

        public static void Main(string[] args)

        {

            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllersWithViews();

            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

            builder.Services.AddEndpointsApiExplorer();

            builder.Services.AddSwaggerGen();

            var app = builder.Build();

            // Configure the HTTP request pipeline.

            if (!app.Environment.IsDevelopment())

            {

                app.UseExceptionHandler("/Home/Error");

            }

            if (app.Environment.IsDevelopment())

            {

                app.UseSwagger();

                //app.UseSwaggerUI();

                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"));

            }

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(

                name: "default",

                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();

        }

    }

}

起用swagger的方法

1、在nuget中导入Swashbuckle.AspNetCore包

2、发布后在web.config中起用,按如下代码格式修改

      <aspNetCore processPath=".\YourApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" >

        <environmentVariables>

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

        </environmentVariables>

      </aspNetCore>

经过上面的操作后,就可以在MVC中加入API及起用Swagger了。


查看更多关于如何在Asp.Net Core Mvc项目中起用api及swagger的详细内容...

  阅读:18次