asp.net webapi post方法返回Object {_body:error,status:0,ok:false,statusText:"",headers:{...},type:3,url:null}

时间:2018-04-08 09:19:12

标签: entity-framework post asp.net-web-api asp.net-core

我正在创建一个带有ef核心的web api - 首先是代码。

网站与HomeSection之间存在一对一的关系,HomeSection与Paragraph之间存在一对一的关系(与HomePara桥接)。

以下是我的模特:

public class Website
{
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
    [Required]
    public string WebsiteUrl { get; set; }

    public Boolean IsDeleted { get; set; }

    public HomeSection Home { get; set; }
}

public class HomeSection
{
    public HomeSection()
    {
        HomeParas = new HashSet<HomePara>();
    }

    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    [MinLength(3)]
    public string Header { get; set; }
    [MinLength(3)]
    public string BackgroundImageUrl { get; set; }
    public Boolean isActive { get; set; }

    public ICollection<HomePara> HomeParas { get; set; }

    public int WebsiteId { get; set; }
    [ForeignKey("WebsiteId")]
    public virtual Website website { get; set; }
}

public class HomePara
{
    public int Id { get; set; }
    public int HomeId { get; set; }
    public int ParagraphId { get; set; }

    public HomeSection Home { get; set; }
    public Paragraph Para { get; set; }
}

public class Paragraph
{
    public Paragraph()
    {
        HomeParas = new HashSet<HomePara>();
    }
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Text { get; set; }

    public ICollection<HomePara> HomeParas { get; set; }
}

我的控制器的post方法: 当用户创建网站时,我想默认创建一个homesection和一个段落。

[Produces("application/json")]
[Route("api/Websites")]
[EnableCors("AllowAny")]
public class WebsitesController : Controller
{
    private readonly WebSiteContext _context;

    public WebsitesController(WebSiteContext context)
    {
        _context = context;
    }

    [HttpPost]
    public async Task<IActionResult> PostWebsite([FromBody] Website website)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        _context.Websites.Add(website);

        //create a default paragraph data and add it
        Paragraph newParagraph = new Paragraph()
        {
            Text = "Home Para"
        };
        _context.Paragraphs.Add(newParagraph);

        //create a default home data and add it
        HomeSection newhome = new HomeSection()
        {
            Header = website.Name + "Head",
            BackgroundImageUrl = website.Name + "bg url",
            WebsiteId = website.Id,
            isActive = true
        };
        _context.HomeSections.Add(newhome);

        //mapping home to para
        newhome.HomeParas.Add(new HomePara()
        {
            HomeId = newhome.Id,
            ParagraphId = newParagraph.Id
        });

        await _context.SaveChangesAsync();
        return CreatedAtAction("GetWebsite", new { id = website.Id }, website);
    }
}

这将返回Object { _body: error, status: 0, ok: false, statusText: "", headers: {…}, type: 3, url: null }而不是201 status

但是正在保存数据,我使用get请求验证了这些数据。

修改 我添加了控制器定义。

我注意到了一些奇怪的事情:

  1. 我将post方法的整个代码放在try catch中,并且执行时没有任何错误。但是,在执行return语句之后,它绑定了Website属性,当它命中HomeSection HomePara时,由于某种原因失败,它会逃避执行流程,并且我得到 0状态
  2. 其次,如果我将帖子方法Website website的原始参数复制到另一个Website,如下所示

    Website newWebsite = new Website() { Id = website.Id, Name = website.Name, WebsiteUrl = website.WebsiteUrl };
    

    然后将此newWebsite传递给CreatedAtAction()方法,

    return CreatedAtAction("GetWebsite", new { id = website.Id }, newWebsite);
    

    然后我获得 201状态。我不知道为什么会这样。怎么解决这个问题?

0 个答案:

没有答案