添加配置管理功能,包括获取和设置配置值的接口及实现
This commit is contained in:
41
WebApi/Controllers/ConfigController.cs
Normal file
41
WebApi/Controllers/ConfigController.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace WebApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class ConfigController(
|
||||
IConfigService configService
|
||||
) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取配置值
|
||||
/// </summary>
|
||||
public async Task<BaseResponse<string>> GetConfig(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var config = await configService.GetConfigByKeyAsync<string>(key);
|
||||
var value = config ?? string.Empty;
|
||||
return value.Ok("配置获取成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return $"获取{key}配置失败: {ex.Message}".Fail<string>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置配置值
|
||||
/// </summary>
|
||||
public async Task<BaseResponse> SetConfig(string key, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
await configService.SetConfigByKeyAsync(key, value);
|
||||
return "配置设置成功".Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return $"设置{key}配置失败: {ex.Message}".Fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user