diff --git "a/\346\236\227\347\216\211\346\225\217/20240520_\351\241\271\347\233\256\345\237\272\347\241\200.md" "b/\346\236\227\347\216\211\346\225\217/20240520_\351\241\271\347\233\256\345\237\272\347\241\200.md" new file mode 100644 index 0000000000000000000000000000000000000000..6b7e4acf8c2cce6ca73c9592455ff2a1615b173f --- /dev/null +++ "b/\346\236\227\347\216\211\346\225\217/20240520_\351\241\271\347\233\256\345\237\272\347\241\200.md" @@ -0,0 +1,29 @@ +# 下载SDK +1. [跳转下载地址](https://dotnet.microsoft.com/zh-cn/download) +2. 下载Vs2022 默认下载SDK + +# 项目 +1. 创建:`dotnet new webapi [-n/-o MyDeom]` +2. 运行:`dotnet run` 或 `http://localhost:5260/swagger` + +/n与/o的区别: +1. /n 命名项目 +2. /o 命名项目,并可以指定路径 +```js +// 创建一个名称为MyDemo的控制台项目 +dotnet new console -n MyDemo +// 创建一个在Demos文件夹下的名称为One的控制台项目 +dotnet new console -o Demos/One +``` + +# 创建一个解决方案,并包含两个项目 +dotnet new webapi -h 获取解释命令符 +1. 新建解决方案`dotnet new sln [-n/-o MySolution]` +2. 将项目添加到解决方案中`dotnet sln add 项目1路径 项目2路径` +3. 列出所有项目`dotnet sln list` + +# Markdown Preview Enhanced 插件 + +[教程:使用 ASP.NET Core 创建 Web API | Microsoft Learn](https://learn.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1&tabs=visual-studio) + +https://blog.csdn.net/ssjdoudou/article/details/107559440 \ No newline at end of file diff --git "a/\346\236\227\347\216\211\346\225\217/20240521_\345\221\275\344\273\244.md" "b/\346\236\227\347\216\211\346\225\217/20240521_\345\221\275\344\273\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..d40b38f83b3e76b09fe08158076e747171a1c89a --- /dev/null +++ "b/\346\236\227\347\216\211\346\225\217/20240521_\345\221\275\344\273\244.md" @@ -0,0 +1,21 @@ +# 终端命令 +1. `dotnet run --project 文件路径` 运行指定文件 +2. `dotnet build` 编译项目sln +3. `dotnet add A reference B` A项目中引入B项目 +4. `dotnet restore/build` 还原/编译 自动下载依赖包 +5. `dotnet watch --project 文件路径` 监视项目(热重载) +6. `dotnet clean` + +# mini api +```cs +// 挂载路由 +app.MapGet("/leaf",async ctx => { + await ctx.Response.WriteAsync("9999"); +}); + +// 使用中间件 +app.Use(async (ctx,next)=>{ + await ctx.Response.WriteAsync("Hi"); + await next(); +}); +``` \ No newline at end of file diff --git "a/\346\236\227\347\216\211\346\225\217/20240523_\345\237\272\347\241\200\346\216\245\345\217\243.md" "b/\346\236\227\347\216\211\346\225\217/20240523_\345\237\272\347\241\200\346\216\245\345\217\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..ebda7e610e736ac820ac5cb5123a6807235a3b21 --- /dev/null +++ "b/\346\236\227\347\216\211\346\225\217/20240523_\345\237\272\347\241\200\346\216\245\345\217\243.md" @@ -0,0 +1,61 @@ +# 抽离 + +# 传统 + +Program.cs +```cs +namespace classDemo; +public static class Programs +{ + public static void Main(string[] args) + { + createHostBuild(args).Build().Run(); + } + public static IHostBuilder createHostBuild (string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(b=> + { + b.UseStartup(); + }); + + } +} +``` + +Startup.cs +```cs +namespace classDemo; +public class Startup{ + // 中间件 + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints=> + { + endpoints.MapControllers(); + }); + } + // 依赖注入容器 + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } +} +``` + +TestApi.cs +```cs +using Microsoft.AspNetCore.Mvc; + +namespace classDemo; +{ + [Route("[controller]")] + public class TestApi:ControllerBase + { + public IActionResult Index() + { + return Ok("333"); + } + } +} +``` \ No newline at end of file diff --git "a/\346\236\227\347\216\211\346\225\217/20240524_\346\216\247\345\210\266\345\231\250\350\267\257\347\224\261.md" "b/\346\236\227\347\216\211\346\225\217/20240524_\346\216\247\345\210\266\345\231\250\350\267\257\347\224\261.md" new file mode 100644 index 0000000000000000000000000000000000000000..43a97c7f199f4f18b376ec13437e3e2df47477e9 --- /dev/null +++ "b/\346\236\227\347\216\211\346\225\217/20240524_\346\216\247\345\210\266\345\231\250\350\267\257\347\224\261.md" @@ -0,0 +1,104 @@ +# 路由 +https://learn.microsoft.com/zh-cn/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 + + 一个方法发送多个请求:`[AcceptVerbs]` +## ApiController +声明ApiController则必须有route +```cs +[ApiController] +[Route("[controller]")] +``` +## Route +当[Route]写在class外部,表示全局通用。写在内部单个方法前,则表示仅当前请求适用。内部route规则会自动拼接外部的route规则 +```cs +// /book +[Route("[controller]")] +public class BookController:ControllerBase +{ + // book/test + [Route("test")] + [HttpGet] + public IActionResult Index(){} + + // 方法重载 + [HttpGet] + public IActionResult Index(int id){} + + // 当方法没有设置[http]时,表示每次请求都会触发该方法 + public IActionResult All(){} + + // /或~ 表示不拼接外部route + // /api/about + [Route("~/api/[action]")] + [Route("/api/[action]")] + [HttpGet] + public IActionResult About() + { + return Ok("34343434"); + } +} +``` +`public RouteAttribute(string template)` +template对应请求路径path: +1. 直接写死 +2. 包含替换指令: + 1. [controller]当前类名 + 2. [action] 指代 方法/Action名 +3. 包含数据:/{atr:type?} +```cs +// 1 用户发送/book +[Route("book")] + +// 2 用户发送 /book/test +[Route("[controller]/[action]")] + +public class BookController:ControllerBase +{ + // 可以是actionName为Test + [HttpGet] + [ActionName("test")] + public IActionResult Index(){} + + // 也可以是Test方法 + [HttpGet] + public IActionResult Test(){} + +// 3 用户发送 /book/1 或者 /book + // {id:int}添加动态参数id, “?”添加可以为空 + [Route("[controller]/{id:int?}")] + [HttpGet] + public ActionResult Home(int id){} +} +``` +## AcceptVerbs + +一个方法适用多种请求 +```cs +[Route("[controller]")] +[AcceptVerbs("GET","POST")] +public ActionResult Edit(int id){} +``` +## Produces +设置返回类型 +```cs +// 请求当前action时,返回的数据格式为xml +[Produces("application/xml")] +[HttpGet] +public IActionResult Index(int id){} + +// 或者在Startup.cs中设置全局数据格式 + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + // 使其能返回Xml格式数据,默认返回json格式 + // 需要设置请求头 Accept:application/xml + services.AddMvcCore().AddXmlSerializerFormatters(); + + // 设置所有返回xml格式 + // services.AddMvcCore(opt=> + // { + // opt.Filters.Add(new ProdcesAttribute("application/xml")); + // }).AddXmlSerializerFormatters(); + } +``` \ No newline at end of file