diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" similarity index 100% rename from "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" rename to "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..8707d9cca185fba0b14c60427ee38157a8bade38 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" @@ -0,0 +1,17 @@ +# 安装REST CLient 插件 + +## 控制器中用法 + +``` +// GET +[HttpGet("{id}")] + +// POST +[HttpPost] + +// PUT +[HttpPut("{id}")] + +// Delete +[HttpDelete("{id}")] +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..7a0a35151d45936a72deb85504ee845d691d4cc6 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210EF Core\357\274\211.md" @@ -0,0 +1,82 @@ +# WebApi 连接数据库 + +# 安装 EF Core + +### 获取 Entity Framework Core 运行时 + +把EF Core添加到项目中,要使用的数据库提供程序的 NuGet 包 + +命名 +``` +// sqlserver/sqlite + +dotnet add package Microsoft.EntityFrameworkCore.Sql数据库类型(sqlserver/sqlite) +``` + +## 数据库迁移 +``` +// 安装dotnet-ef +dotnet tool install --global dotnet-ef + +// 安装EFCore.Design +dotnet add package Microsoft.EntityFrameworkCore.Design + +// 创建迁移,指示 EF Core 创建名为 InitialCreate 的迁移 +dotnet ef migrations add InitialCreate + +// 创建数据库和架构 +dotnet ef database update +``` + +创建模型 Data 部分配置演示 + +``` +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; + +namespace EFGetStarted +{ + public class BloggingContext : DbContext + { + public DbSet Blogs { get; set; } + public DbSet Posts { get; set; } + + // The following configures EF to create a Sqlite database file as `C:\blogging.db`. + // For Mac or Linux, change this to `/tmp/blogging.db` or any other absolute path. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite(@"Data Source=C:\blogging.db"); + } + + public class Blog + { + public int BlogId { get; set; } + public string Url { get; set; } + + public List Posts { get; } = new List(); + } + + public class Post + { + public int PostId { get; set; } + public string Title { get; set; } + public string Content { get; set; } + + public int BlogId { get; set; } + public Blog Blog { get; set; } + } +} +``` +创建数据库 + ++ 以下步骤使用迁移创建数据库 + +命令 +``` +dotnet tool install --global dotnet-ef + +dotnet add package Microsoft.EntityFrameworkCore.Design + +dotnet ef migrations add InitialCreate + +dotnet ef database update +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" new file mode 100644 index 0000000000000000000000000000000000000000..2525f9245f0a33de5a2b0ae95d235f464d08ce96 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api.sln" @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web.Api", "Web.Api\Web.Api.csproj", "{2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x64.ActiveCfg = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x64.Build.0 = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x86.ActiveCfg = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Debug|x86.Build.0 = Debug|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|Any CPU.Build.0 = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x64.ActiveCfg = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x64.Build.0 = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x86.ActiveCfg = Release|Any CPU + {2FEFD2BA-1065-4863-ABBD-9DF4DDBEBE72}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..10625f771d1c6882c62b127b0603c0ce36f49159 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/UsersController.cs" @@ -0,0 +1,66 @@ +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using Web.Api.Entity; +using System.Linq; + +namespace Web.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase{ + [HttpGet] + public dynamic Get(){ + var users = GetUsers(); + return users; + } + [HttpGet("{id}")] + public dynamic Get(int id){ + var users = GetUsers(); + var user = users.Where(x=>x.id==id).FirstOrDefault(); + return user; + } + [HttpPost("{id}")] + public dynamic Post(dynamic model){ + return new { + Code = 2000, + Date = model, + Msg = "成功", + }; + } + [HttpPut("{id}")] + public dynamic Put(int id, dynamic model){ + return new{ + Date = model, + Msg=string.Format("要修改的ID为{0},",id) + }; + } + [HttpDelete("{id}")] + public dynamic Delete(int id,dynamic model){ + return new{ + Date = model, + Msg=string.Format("要修改的ID为{0},",id) + }; + } + + public IEnumerable GetUsers(){ + var users = new List{ + new Users{ + Id=1, + Username="good", + Password="lisa" + }, + new Users{ + Id=2, + Username="good", + Password="nice" + }, + new Users{ + Id=3, + Username="good", + Password="wdnbd" + }, + }; + return users; + } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3b56fb4ceced66bed76d8ce0fbe508e80e04c9d9 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace Web.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Date/Admin3000Db.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Date/Admin3000Db.cs" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..9a7404e7454772acd073f2f85c3535bb04298184 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Entity/Users.cs" @@ -0,0 +1,17 @@ +using System; +namespace Web.Api.Entity +{ + public class Users{ + public Users(){ + createdTime = DateTime.Now; + updatedTime = DateTime.Now; + Remarks = ""; + } + public int Id {get;set;} + public string Username {get;set;} + public string Password {get;set;} + public DateTime createdTime{get;set;} + public DateTime updatedTime{get;set;} + public string Remarks {get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..11cd3c83ea7fb0552ad024dac39cae3e818b464d --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Web.Api +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..e36e127923f1aeb8a7883fb2f8a06a7e7a401874 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:42799", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Web.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..009492d39a8856048d33be97a86cd5f0f6dee09a --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Startup.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Web.Api +{ + 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.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(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..699931c1b60e4fce32251be36828d8f57fb09cfe --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace Web.Api +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..2efd60a756eb6317d8230f6488cca9e3f989bbbf --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/Web.Api.csproj" @@ -0,0 +1,13 @@ + + + + netcoreapp3.1 + + + + + + + + + diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/Web.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" "b/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..a84c8d1d308c3531c79e4dbd51216a664f35853d --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Web.Api/api.http" @@ -0,0 +1,16 @@ +###获取用户列表 +GET http://localhost:5000/users HTTP/1.1 + + +###获取指定ID用户 +GET http://localhost:5000/users3 HTTP/1.1 + +###创建用户 +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "username":"good", + "password":"土笨笨" +} +