--- { title: "LagrangeCoreApi 开发小记", description: "本文介绍了C#中ControllerBase的扩展方法,通过重载设计解决响应中数据与消息参数组合的问题。同时提到Lagrange.Core现已提供官方签名API,无需自建签名服务。最后分享了使用VS的Git功能时,建议以命令行为主、界面为辅的操作经验。", draft: false, type: "article", created_at: "2024-07-07T21:00:00+08:00", published_at: "2024-07-07T21:09:00+08:00", updated_at: [ "2024-07-07T21:09:00+08:00"], category: 'Uni', tags: [ "C#" ], tech_stack: [ "C#" ], tech_stack_percent: [ 1 ], tech_stack_icon_names: [ "mdi:language-csharp" ], tech_stack_theme_colors: [ "#a179dc" ], } --- !!!warning Legacy Article 过时的文章 此文章从旧博客迁移而来,编写时技术水平有限,仅供参考 !!! ```C# public static class ControllerBaseExtension { public static IActionResult Success(this ControllerBase controller) => new ApiResponse(200); public static IActionResult Success(this ControllerBase controller, string msg) => new ApiResponse(200, msg); public static IActionResult Success(this ControllerBase controller, T data) => new ApiResponse(200, "", data); public static IActionResult Success( this ControllerBase controller, string msg, T? data ) => new ApiResponse(200, msg, data); public static IActionResult Fail(this ControllerBase controller) => new ApiResponse(500); public static IActionResult Fail(this ControllerBase controller, string msg) => new ApiResponse(500, msg); } ``` 之前写这段代码时遇到一个问题,如果想要使用一个参数,发送string类型的data,但msg留空。在全用范型方法的情况下是做不到的。当时把这个问题暂时搁置了。但后续使用方法时,由于忘记填写范型类型导致报错,让我想出了如上的方法。如果有范型则最后一个参数为data。否则没有data数据。后续可能会用object和null代替。 第二,Lagrange.Core现在有官方的Api地址,可以不用自己设置sign服务了 https://sign.lagrangecore.org/api/sign Thanks for 外国热心网友 for Provision of Azure Servlet ** Built-in SignServer is now provided, Enjoy! ** 第三,VS的git真的难用。点了半天后上网搜了git命令直接解决了。建议只把VS的git作为一个可视化工具。