Asp.net core ASP.Net核心web API将字符串编码为base64
我不熟悉.Net核心开发。我有一个模型:Asp.net core ASP.Net核心web API将字符串编码为base64,asp.net-core,asp.net-core-webapi,Asp.net Core,Asp.net Core Webapi,我不熟悉.Net核心开发。我有一个模型: public class CoreGoal { [Key] public long CoreGoalId { get; set; } public string Title { get; set; } public string Effect { get; set; } public string Target_Audience { get; set; } public string Infrastruct
public class CoreGoal
{
[Key]
public long CoreGoalId { get; set; }
public string Title { get; set; }
public string Effect { get; set; }
public string Target_Audience { get; set; }
public string Infrastructure { get; set; }
public virtual ICollection<Image> Images { get; set; }
public CoreGoal()
{
}
}
我正在使用存储库模式。我的存储库:
public interface ICoreGoalRepository
{
void CreateCoreGoal(CoreGoal coreGoal);
}
public class CoreGoalRepository : ICoreGoalRepository
{
private readonly WebAPIDataContext _db;
public CoreGoalRepository(WebAPIDataContext db)
{
_db = db;
}
//Find specific
public CoreGoal Find(long key)
{
return _db.CoreGoals.FirstOrDefault(t => t.CoreGoalId == key);
}
//Add new
public void CreateCoreGoal(CoreGoal coreGoal)
{
_db.CoreGoals.Add(coreGoal);
_db.SaveChanges();
}
}
和控制器:
[Route("api/[controller]")]
public class CoreGoalController : Controller
{
private readonly ICoreGoalRepository _coreGoalRepository;
//Controller
public CoreGoalController(ICoreGoalRepository coreGoalRepository) {
_coreGoalRepository = coreGoalRepository;
}
[HttpGet("{id}", Name = "GetCoreGoal")]
public IActionResult GetById(long id)
{
var item = _coreGoalRepository.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
//Create
[HttpPost]
public IActionResult Create([FromBody] CoreGoal item)
{
if (item == null)
{
return BadRequest();
}
_coreGoalRepository.CreateCoreGoal(item);
return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
}
}
在对CoreGoal的POST请求中——在创建新的CoreGoal时,我想将图像模型的Base64属性从字符串转换为字节[]。我找到了这篇()博客文章,但我不确定我应该在哪里写这段代码
有人能帮我吗 开始时,您应该更改数据库模型,将二进制图像保存到db(同样,这仍然不是一个好主意,但让我们暂时搁置它): 接下来,您只需在控制器内转换图像:
[HttpPost]
public IActionResult Create([FromBody] CoreGoal item)
{
if (item == null)
{
return BadRequest();
}
item.Binary = Convert.FromBase64String(item.Base64);
_coreGoalRepository.CreateCoreGoal(item);
return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
}
顺便说一句:你的代码还是不好。没有必要将存储库模式与EF core()一起使用。您应该引入两个模型层:
公共层
和模型层
。您不应该将EF Core contract公开给外界。我还不清楚这个问题,您能提供更多详细信息吗?因此,基本上我的CoreGoal可以有许多图像,这就是为什么我将其作为一个单独的模型创建。现在,图像模型有一个类型为string的属性Base64。当我创建一个CoreGoal时,我还发布了base64字符串,不幸的是,在保存到MySql数据库时,这个大字符串被切掉了一半。所以有人建议我不要使用字符串,而是使用byte[]作为数据类型。但如果我将其更改为byte[],我的POST请求将失败,状态为400。因此,也许不需要将其改为byte[]编码就可以了。可能重复的
public class Image
{
[Key]
public long ImagelId { get; set; }
[NotMapped]
public string Base64 { get; set; }
public byte[] Binary {get; set;}
[ForeignKey("CoreGoalId")]
public long CoreGoalId { get; set; }
public Image()
{
}
}
[HttpPost]
public IActionResult Create([FromBody] CoreGoal item)
{
if (item == null)
{
return BadRequest();
}
item.Binary = Convert.FromBase64String(item.Base64);
_coreGoalRepository.CreateCoreGoal(item);
return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
}