BlogArticles/2024-07/LagrangeCoreApi 开发小记.md

55 lines
2.4 KiB
Markdown
Raw Normal View History

2025-08-27 11:19:39 +08:00
---
{
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<int>(200);
public static IActionResult Success(this ControllerBase controller, string msg) =>
new ApiResponse<int>(200, msg);
public static IActionResult Success<T>(this ControllerBase controller, T data) =>
new ApiResponse<T>(200, "", data);
public static IActionResult Success<T>(
this ControllerBase controller,
string msg,
T? data
) => new ApiResponse<T>(200, msg, data);
public static IActionResult Fail(this ControllerBase controller) =>
new ApiResponse<int>(500);
public static IActionResult Fail(this ControllerBase controller, string msg) =>
new ApiResponse<int>(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作为一个可视化工具。