当前位置:首页 > Web开发 > 正文

获取一条记录 #p#分页标题#e# 1 using System; 2 using System.Collections

2024-03-31 Web开发

主要参考:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio

1、环境配置

想要使用.net core3.1 第一个问题是更新VS,更新过程中遇到报错

VS_InstallerShell.exe has an invalid certificate. Please ensure the appropriate Microsoft certificates are installed

网上很多要领试过没有效果,最后在哪里(本身也忘记了)找到了解决方案,安置两个windows补丁,附上补丁编号和下载地点

https://www.catalog.update.microsoft.com/Home.aspx

KB4474419
kb4490628

2.措施编写

安置了最新vs以后,点击新建项目,创建3.1 .net core API项目,创建以后可以直接运行,会有一个天气预报的示例

使用Nuget安置Microsoft.EntityFrameworkCore.SqlServr,,ef我虽然没有在实际项目中使用过,不过陆陆续续知道点,今天趁便尝尝

2.1 创建数据库上下文类

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.Data.SqlClient; 6 using Microsoft.EntityFrameworkCore; 7 8 namespace reportAPI 9 { 10 public class ChartDesignContenxt: DbContext 11 { 12 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 13 { 14 var sqlConnectionStringBuilder = new SqlConnectionStringBuilder 15 { 16 DataSource = "*.*.*.*", 17 InitialCatalog = "*", 18 UserID = "sa", 19 Password = "123456" 20 }; 21 optionsBuilder.UseSqlServer(sqlConnectionStringBuilder.ConnectionString); 22 23 base.OnConfiguring(optionsBuilder); 24 } 25 public DbSet<chartDesign> chartDesigns { get; set; } 26 } 27 }

2.2 在startup.cs类中注入数据库上下文类,添加跨域配置

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Builder; 6 using Microsoft.AspNetCore.Hosting; 7 using Microsoft.AspNetCore.HttpsPolicy; 8 using Microsoft.AspNetCore.Mvc; 9 using Microsoft.Extensions.Configuration; 10 using Microsoft.Extensions.DependencyInjection; 11 using Microsoft.Extensions.Hosting; 12 using Microsoft.Extensions.Logging; 13 14 namespace reportAPI 15 { 16 public class Startup 17 { 18 public Startup(IConfiguration configuration) 19 { 20 Configuration = configuration; 21 } 22 23 public IConfiguration Configuration { get; } 24 25 // This method gets called by the runtime. Use this method to add services to the container. 26 public void ConfigureServices(IServiceCollection services) 27 { 28 //允许一个或多个具体来源: 29 services.AddCors(options => 30 { 31 // 配置跨域 32 options.AddPolicy("cors", policy => 33 { 34 // 设定允许跨域的来源,有多个的话可以用 `,` 离隔 35 policy 36 .AllowAnyOrigin() 37 .AllowAnyHeader() 38 .AllowAnyMethod(); 39 }); 40 }); 41 42 services.AddScoped<IPMSDbContenxt>(_ => new IPMSDbContenxt()); 43 services.AddScoped<ChartDesignContenxt>(_ => new ChartDesignContenxt()); //注入数据库上下文类 44 45 services.AddControllers(); 46 } 47 48 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 49 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 50 { 51 if (env.IsDevelopment()) 52 { 53 app.UseDeveloperExceptionPage(); 54 } 55 56 app.UseHttpsRedirection(); 57 58 app.UseRouting(); 59 60 app.UseCors("cors"); //使用跨域 61 62 app.UseAuthorization(); 63 64 app.UseEndpoints(endpoints => 65 { 66 endpoints.MapControllers(); 67 }); 68 } 69 } 70 }

2.3 创建控制器,一共三个要领,获取列表,更新一笔记录,获取一笔记录

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32168.html