ASP.NET核心POST请求失败

时间:2017-04-06 07:41:51

标签: asp.net-core entity-framework-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<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()
    {

    }
 }

图像模型如下:

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

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

    public Image()
    {

    }
} 

我的控制器类:

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

}

存储库:

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

当我从swagger发出POST请求时,我得到一个模板,如:

{
  "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
        }
      ]
    }
  ]
}

如果我像这样发帖,我的请求将失败,状态为400,但如果我删除

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

从这个请求,然后它是成功的。为什么会这样?所有其他模型,即Benefit,Step与结构中的Image完全相同。

更新 将base64类型从byte []更改为string可以消除此问题,但在这种情况下,在保存到MySql数据库时,大的base64字符串被切断,再次形成图像变得无用。

0 个答案:

没有答案