From 66b7c6dad91e52ffc30a607aea7a610a11d9124f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=B2=E6=98=8C=E6=9C=88?= <2821684800@qq.com> Date: Sun, 26 May 2024 14:55:34 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蒲昌月 <2821684800@qq.com> --- ...345\273\272\351\246\226\344\270\252Api.md" | 25 ++++++++ ...73\345\212\240\351\241\271\347\233\256.md" | 18 ++++++ ...35\350\265\226\346\263\250\345\205\245.md" | 59 +++++++++++++++++++ ...72\346\216\247\345\210\266\345\231\250.md" | 58 ++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 "\350\222\262\346\230\214\346\234\210/20240520-\345\210\233\345\273\272\351\246\226\344\270\252Api.md" create mode 100644 "\350\222\262\346\230\214\346\234\210/20240521-\350\247\243\345\206\263\346\226\271\346\241\210\357\274\214\345\274\225\347\224\250\346\267\273\345\212\240\351\241\271\347\233\256.md" create mode 100644 "\350\222\262\346\230\214\346\234\210/20240523-api\346\212\275\347\246\273\343\200\201\350\275\254\346\215\242\343\200\201\344\276\235\350\265\226\346\263\250\345\205\245.md" create mode 100644 "\350\222\262\346\230\214\346\234\210/20240524-api\345\210\233\345\273\272\346\216\247\345\210\266\345\231\250.md" diff --git "a/\350\222\262\346\230\214\346\234\210/20240520-\345\210\233\345\273\272\351\246\226\344\270\252Api.md" "b/\350\222\262\346\230\214\346\234\210/20240520-\345\210\233\345\273\272\351\246\226\344\270\252Api.md" new file mode 100644 index 0000000..de896c2 --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240520-\345\210\233\345\273\272\351\246\226\344\270\252Api.md" @@ -0,0 +1,25 @@ +## 创建首个Api + +1. 安装SDK + +2. Web的命令: + +- 新建一个WebApi项目:dotnet new webapi -n `命名` +- 新建一个解决方案:dotnet new sln -n `命名` +- 新建一个WebApi项目(--no -https参数表示不启用https):dotnet new webapi --no -https -o dream.WebApi +- 创建类库:dotnet new classlib -o `命名(dream.Entity)` + +3. 运行这个项目 +- dotnet run + +4. 在WeatherForecast.js中复制类名至导航栏,可看到数据 + +> 导航栏中localhost:端口/`swagger`可进入api的页面(CRUD) \ No newline at end of file diff --git "a/\350\222\262\346\230\214\346\234\210/20240521-\350\247\243\345\206\263\346\226\271\346\241\210\357\274\214\345\274\225\347\224\250\346\267\273\345\212\240\351\241\271\347\233\256.md" "b/\350\222\262\346\230\214\346\234\210/20240521-\350\247\243\345\206\263\346\226\271\346\241\210\357\274\214\345\274\225\347\224\250\346\267\273\345\212\240\351\241\271\347\233\256.md" new file mode 100644 index 0000000..6e74774 --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240521-\350\247\243\345\206\263\346\226\271\346\241\210\357\274\214\345\274\225\347\224\250\346\267\273\345\212\240\351\241\271\347\233\256.md" @@ -0,0 +1,18 @@ +## 解决方案,引用添加项目 +### 创建解决方案的sln文件 +1. 创建的命令 +- dotnet new sln -n `命名` +2. 添加项目的命令 +- dotnet sln add `项目文件夹` + +### 文件之间的引用 +- dotnet add `引用位置` reference `被引用文件` + +### 添加依赖包 +- dotnet add package `包名` + +### 还原文件 +- dotnew restore/build(优先) + +### 将项目跑起来 +- dotnet watct/run --project `文件位置` diff --git "a/\350\222\262\346\230\214\346\234\210/20240523-api\346\212\275\347\246\273\343\200\201\350\275\254\346\215\242\343\200\201\344\276\235\350\265\226\346\263\250\345\205\245.md" "b/\350\222\262\346\230\214\346\234\210/20240523-api\346\212\275\347\246\273\343\200\201\350\275\254\346\215\242\343\200\201\344\276\235\350\265\226\346\263\250\345\205\245.md" new file mode 100644 index 0000000..53e07ae --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240523-api\346\212\275\347\246\273\343\200\201\350\275\254\346\215\242\343\200\201\344\276\235\350\265\226\346\263\250\345\205\245.md" @@ -0,0 +1,59 @@ +## api抽离、转换、依赖注入 + +- Program +```cs +namespace Admin.api; + +public static class Program +{ + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + public static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(builder=> + { + builder.UseStartup(); + }); + } +} +``` + +- Startup +```cs +namespace Admin.api; + +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints =>{ + endpoints.MapControllers(); + }); + } + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } +} +``` + +- BlogController +```cs +using Microsoft.AspNetCore.Mvc; + +namespace Admin.api; + +[Route("[controller]")] + +public class BlogsController:ControllerBase +{ + public IActionResult Index() + { + return Ok("会了吗???"); + } +} +``` \ No newline at end of file diff --git "a/\350\222\262\346\230\214\346\234\210/20240524-api\345\210\233\345\273\272\346\216\247\345\210\266\345\231\250.md" "b/\350\222\262\346\230\214\346\234\210/20240524-api\345\210\233\345\273\272\346\216\247\345\210\266\345\231\250.md" new file mode 100644 index 0000000..034aee7 --- /dev/null +++ "b/\350\222\262\346\230\214\346\234\210/20240524-api\345\210\233\345\273\272\346\216\247\345\210\266\345\231\250.md" @@ -0,0 +1,58 @@ +## api创建控制器 +- Program +```cs +namespace Admin.api; + +public static class Program +{ + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + public static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(builder=> + { + builder.UseStartup(); + }); + } +} +``` + +- Startup +```cs +namespace Admin.api; + +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints =>{ + endpoints.MapControllers(); + }); + } + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } +} +``` + +- BlogController +```cs +using Microsoft.AspNetCore.Mvc; + +namespace Admin.api; + +[Route("[controller]")] + +public class BlogsController:ControllerBase +{ + public IActionResult Index() + { + return Ok("会了吗???"); + } +} +``` \ No newline at end of file -- Gitee