NetCore WebApi 及 搭建Swagger

 十年河东,十年河西,莫欺少年穷

学无止境,精益求精

首先,本篇教程为基础教程,大牛请翻过,小白值得一看

如下:

1、准备数据

create database StudentDB
go
use StudentDB
go 
create table Student
(
StudentId varchar(50) primary key,
StudentSex nvarchar(2),
StudentName nvarchar(50),
StudentAge int,
CreateDate datetime
)

insert into Student
values(newid(),'','王娜',18,getdate())

insert into Student
values(newid(),'','李丽',19,getdate())

insert into Student
values(newid(),'','阿卜杜拉',17,getdate())

insert into Student
values(newid(),'','高骏花',20,getdate())

insert into Student
values(newid(),'','陈雪好',18,getdate())

insert into Student
values(newid(),'','刘邦',18,getdate())

insert into Student
values(newid(),'','陈世美',16,getdate())

insert into Student
values(newid(),'','陈学德',20,getdate())

insert into Student
values(newid(),'','陈夏明',18,getdate())

insert into Student
values(newid(),'','龙德海',19,getdate())

创建一个数据库,一张数据表,十条数据,这~没什么好说的

2、打开VS2019、创建一个空白解决方案 Student,如下:

NetCore WebApi 及 搭建Swagger

 

 3、在项目中创建一个NetCore类库,并命名为:StudentContext

NetCore WebApi 及 搭建Swagger

3.1、打开程序包管理器控制台(工具->NuGet包管理器->程序包管理器控制台),执行如下命令:

   Install-Package Microsoft.EntityFrameworkCore
   Install-Package Microsoft.EntityFrameworkCore.SqlServer
   Install-Package Microsoft.EntityFrameworkCore.Tools
   Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design

待四个安装包依次安装成功后,我们执行生成数据库上下文的命令,如下:

Scaffold-DbContext "Data Source=DESKTOP-VD79APR\SQLEXPRESS;Initial Catalog=StudentDB;User ID=sa;Password=chen1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Forc

这样,该项目下,就会生成用于数据库访问的上下文类及各个数据表对应的实体,如下:

NetCore WebApi 及 搭建Swagger

 

 

4、添加NetCore  Web项目,并命名为:StudentWeb

NetCore WebApi 及 搭建Swagger

 

 

 在StartUp.cs中注册数据库服务,如下:

NetCore WebApi 及 搭建Swagger

整个Startup.cs代码如下(后续不在贴出该类的代码):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using StudentContext.Models;
using StudentInterface;
using StudentService;
//
using System.IO;
using System.Reflection;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;

namespace StudentWeb
{
    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.AddScoped<IStudentService, StudentBll>();
            //
            //注册MVC应用
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            //添加数据库上下文
            services.AddDbContext<StudentDBContext>(options =>
                     options.UseSqlServer(Configuration.GetConnectionString("StudentDBContext")));
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
            });
            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();
            }

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
View Code

相关文章:

  • 2021-10-22
  • 2022-12-23
  • 2022-02-19
  • 2022-02-02
  • 2022-01-10
  • 2022-02-12
  • 2021-11-25
猜你喜欢
  • 2021-07-30
  • 2021-06-05
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
相关资源
相似解决方案