【发布时间】:2019-11-14 06:14:44
【问题描述】:
我们有一个 API 请求为一家公司发表评论:
POST http://localhost:4249/api/calendar/comments HTTP/1.1
Host: localhost:4249
Connection: keep-alive
Content-Length: 67
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:4249
X-Requested-With: XMLHttpRequest
Content-Type: application/json
{"CompanyId":"9e21bb54-387e-428a-878b-04cd0f9cc0d3","Text":"company comment text"}
如果目前没有我们想发表评论的公司,我们应该返回404 还是400?
另外,如果我们以这种方式重组请求
http://localhost:4249/api/calendar/comments?companyid=9e21bb54-387e-428a-878b-04cd0f9cc0d3
Host: localhost:4249
Connection: keep-alive
Content-Length: 67
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:4249
X-Requested-With: XMLHttpRequest
Content-Type: application/json
{ "Text":"company comment text" }
应该更好吗?
Company 和 CompanyComment 的 EF 类是:
public class CompanyComment
{
[Key]
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; }
[Required]
public string Text { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
[Required]
public Guid CompanyId { get; set; }
public virtual Company Company { get; set; }
}
public class Company
{
[Key]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public DateTime Created { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public virtual ICollection<CompanyComment> Comments { get; set; }
}
【问题讨论】:
标签: json rest api http httpwebrequest