diff --git "a/\345\210\230\344\270\234\345\275\252/2024.05.20-\345\210\233\345\273\272api\351\241\271\347\233\256.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.20-\345\210\233\345\273\272api\351\241\271\347\233\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..a2e21b0f96be19a81a3ec7d4e9a028dacacfa15e --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.20-\345\210\233\345\273\272api\351\241\271\347\233\256.md" @@ -0,0 +1,17 @@ +## 创建首个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)` +- 运行(命令当中 “-p” 是指定要运行的项目):dotnet run --project Dream.WebApi/Dream.WebApi.csproj --> +3. 运行这个项目 +- dotnet run + +4. 在WeatherForecast.js中复制类名至导航栏,可看到数据 + +> 导航栏中localhost:端口/`swagger`可进入api的页面(CRUD) \ No newline at end of file diff --git "a/\345\210\230\344\270\234\345\275\252/2024.05.21-\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/\345\210\230\344\270\234\345\275\252/2024.05.21-\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 0000000000000000000000000000000000000000..6e7477453c5546d4f494bca72d04031d7e56bd5e --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.21-\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/\345\210\230\344\270\234\345\275\252/2024.05.23-api\346\212\275\347\246\273\357\274\214\350\275\254\346\215\242\357\274\214\344\276\235\350\265\226\346\263\250\345\205\245.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.23-api\346\212\275\347\246\273\357\274\214\350\275\254\346\215\242\357\274\214\344\276\235\350\265\226\346\263\250\345\205\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..53e07aeed5748e7b478febff621dc3a2fdf5b2a0 --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.23-api\346\212\275\347\246\273\357\274\214\350\275\254\346\215\242\357\274\214\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/\345\210\230\344\270\234\345\275\252/2024.05.24-api\345\210\233\345\273\272\346\216\247\345\210\266\345\231\250.md" "b/\345\210\230\344\270\234\345\275\252/2024.05.24-api\345\210\233\345\273\272\346\216\247\345\210\266\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..034aee77189315b88c629945f0afd05c957e0d58 --- /dev/null +++ "b/\345\210\230\344\270\234\345\275\252/2024.05.24-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