ASP.NET core POST request fail

旧巷老猫 提交于 2019-12-11 06:51:35

问题


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<Benefit> Benefits { get; set; }
    public virtual ICollection<Step> Steps { get; set; }
    public virtual ICollection<Image> Images { get; set; }
    public virtual ICollection<SubGoal> SubGoals { get; set; }

    public CoreGoal()
    {

    }
 }

And Image model is as following:

public class Image
{
    [Key]
    public long ImagelId { get; set; }
    public byte[] Base64 { get; set; }

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

    public Image()
    {

    }
} 

My controller class:

[Route("api/[controller]")]
public class CoreGoalController : Controller
{
    private readonly ICoreGoalRepository _coreGoalRepository;

    //Controller
    public CoreGoalController(ICoreGoalRepository coreGoalRepository) {
        _coreGoalRepository = coreGoalRepository;
    }

    //Get methods
    [HttpGet]
    public IEnumerable<CoreGoal> GetAll()
    {
        return _coreGoalRepository.GetAllCoreGoals();
    }

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

}

Repository:

public class CoreGoalRepository : ICoreGoalRepository
{
    private readonly WebAPIDataContext _db;

    public CoreGoalRepository(WebAPIDataContext db)
    {
        _db = db;
    }

    //Add new
    public void CreateCoreGoal(CoreGoal coreGoal)
    {
        _db.CoreGoals.Add(coreGoal);
        _db.SaveChanges();
    }

    //Get all
    public IEnumerable<CoreGoal> GetAllCoreGoals()
    {
        return _db.CoreGoals
            .Include(coreGoal => coreGoal.Benefits)
            .Include(coreGoal => coreGoal.Steps)
            .Include(coreGoal => coreGoal.Images)
            .Include(coreGoal => coreGoal.SubGoals)
            .ToList();
    }

    //Find specific
    public CoreGoal Find(long key)
    {
        return _db.CoreGoals.FirstOrDefault(t => t.CoreGoalId == key);
    }

}

public interface ICoreGoalRepository
{
    void CreateCoreGoal(CoreGoal coreGoal);
    IEnumerable<CoreGoal> GetAllCoreGoals();
    CoreGoal Find(long key);
    void DeleteCoreGoal(long id);
    void UpdateCoreGoal(CoreGoal coreGoal);
}

When I do a POST request from swagger I get a template like:

{
  "coreGoalId": 0,
  "title": "string",
  "effect": "string",
  "target_Audience": "string",
  "infrastructure": "string",
  "benefits": [
    {
      "benefitId": 0,
      "what": "string",
      "coreGoalId": 0
    }
  ],
  "steps": [
    {
      "stepId": 0,
      "what": "string",
      "coreGoalId": 0
    }
  ],
  "images": [
    {
      "imagelId": 0,
      "base64": "string",
      "coreGoalId": 0
    }
  ],
  "subGoals": [
    {
      "subGoalId": 0,
      "title": "string",
      "priority": "string",
      "audience": "string",
      "social_aspects": "string",
      "coreGoalId": 0,
      "issues": [
        {
          "issueId": 0,
          "title": "string",
          "subGoalID": 0
        }
      ]
    }
  ]
}

If I POST like like this, my request fails with status 400, however if I remove

  "images": [
    {
      "imagelId": 0,
      "base64": "string",
      "coreGoalId": 0
    }
  ],

from this request, then it is successful. Why is it happening? All other models i.e. Benefit, Step are exactly identical to Image in structure.

UPDATE: Changing base64 type from byte[] to string eliminates this problem but in that case while saving to my MySql database the big base64 string is chopped and kind of becomes useless to again form the image.

来源:https://stackoverflow.com/questions/43248760/asp-net-core-post-request-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!