ASP.Net核心Web API将字符串编码为base64

时间:2017-04-06 08:51:59

标签: asp.net-core asp.net-core-webapi

我是.Net Core开发的新手。我有一个模特:

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 class Image
{
    [Key]
    public long ImagelId { get; set; }
    public string Base64 { get; set; }

    [ForeignKey("CoreGoalId")]
    public long CoreGoalId { get; set; }

    public Image()
    {

    }
}

我正在使用Repository模式。我的存储库:

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时,我想将Image model的Base64属性从string转换为byte []。我找到了这个(https://adrientorris.github.io/aspnet-core/manage-base64-encoding.html)博文,但我不确定我应该在哪里编写这段代码。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

最初,您应该更改数据库模型以将二进制映像保存到db(而且,这仍然不是一个好主意,但让我们暂时保留它):

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);
}

顺便说一句:您的代码仍然不好。无需将存储库模式与EF核心(https://www.thereformedprogrammer.net/is-the-repository-pattern-useful-with-entity-framework-core/)一起使用。并且您应该引入两个模型层:public layermodel layer。您不应将EF Core合同暴露在外部。