ASP.Net核心Web API将字符串编码为base64 [英] ASP.Net core web API encode string to base64

查看:349
本文介绍了ASP.Net核心Web API将字符串编码为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to .Net Core development. I have a model:

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()
    {

    }
}

我正在使用存储库模式.我的资料库:

I am using Repository pattern. My 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模型的Base64属性从字符串转换为byte [].我发现了这个( https://adrientorris.github.io/aspnet- core/manage-base64-encoding.html )博客文章,但是我不确定我应该在哪里编写这段代码.

On POST request for CoreGoal- While creating a new CoreGoal, I would like to convert Image model's Base64 attribute from string to byte[]. I found this (https://adrientorris.github.io/aspnet-core/manage-base64-encoding.html) blogpost, but I am not sure where Am I supposed to write this piece of code.

有人可以帮助我吗?

推荐答案

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

Initially you should chage you database model to save you binary image to db (also, it's still not good idea, but let leave it for a now):

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()
   {

   }
}

接下来,您只应该在控制器内部转换图像:

next you just should convert your image inside controller:

[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核心一起使用(

BTW:you code still not good. It's not necessary to use Repository pattern with EF core (https://www.thereformedprogrammer.net/is-the-repository-pattern-useful-with-entity-framework-core/). And you should introduce two model layers: public layer and model layer. You shouldn't expose EF Core contract to outside.

这篇关于ASP.Net核心Web API将字符串编码为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆