提问人:Winter Wind 提问时间:9/7/2023 最后编辑:Winter Wind 更新时间:9/8/2023 访问量:47
在 json base64 值中添加类型和字符集
Adding type and charset in json base64 value
问:
我需要更改json正文响应:
{
"id": "1581fbd2-c045-4162-9f4c-ddbca6a88d61",
"content": "//content in base64"
"title": "ProfileImage",
"type": "image/jpeg",
"created": "2023-08-03T18:55:46.736405+03:00"
}
“content”字段如下所示:
"content": "Data: image/jpeg; Charset: utf-8;//content in base64"
有没有办法在映射配置中做到这一点?
尝试直接在控制器中添加值
var result = await _mediaService.GetById(id);
if (result == null)
{
return NotFound();
}
string base64Data = Convert.ToBase64String(result.Content);
string metaInfo = string.Format("Data: {0}; Charset: {1}", result.Type, Encoding.UTF8.WebName);
string base64String = metaInfo + Convert.ToBase64String(result.Content);
var mapResult = _mapper.Map<MediaModelResponse>(result);
var response = new
{
mapResult.Id,
Content = base64String,
mapResult.Title,
mapResult.Created
};
return Ok(response);
它有效,但我想找到更好的解决方案
还尝试在我的模型中添加一个新字段“ContentTest”并直接写入结果
mapResult.ContentTest = base64String;
答:
1赞
Alexander
9/7/2023
#1
您可以将转换逻辑移动到配置中base64
Automapper
cfg.CreateMap<MediaDto, MediaModelResponse>()
.ForMember(i => i.Content, cfg => cfg.MapFrom((src, dest) =>
{
string metaInfo = string.Format("Data: {0}; Charset: {1}", src.Type, Encoding.UTF8.WebName);
string base64String = metaInfo + Convert.ToBase64String(src.Content);
return base64String;
}));
上一个:合并 2 个 json 值
下一个:提高 GitLab 流水线性能
评论
_mapper.Map<MediaModelResponse>(result);
mapResult
response