【发布时间】:2019-02-27 10:25:40
【问题描述】:
/// <summary>
/// updates information of a job
/// </summary>
/// <param name="job"></param>
/// <returns>Ok or Error</returns>
/// <example>
/// {
/// "info_id": 1,
/// "some_other_id": 2
/// }
/// </example>
[HttpPost]
[Route("api/job/update")]
public IHttpActionResult update(Models.Job job) {
}
//the model
public class Job {
[Required(AllowEmptyStrings = false)]
[Range(1, Int64.MaxValue)]
public Int64 info_id { get; set; }
public Int64? some_other_id{ get; set; }
public DateTime last_log_time { get; set; }
}
想象一下上面的设置。我想在文档中展示在update 的文档块中编写的示例 JSON。但是,这里显示了类型为 Job 的对象的序列化 JSON,而改为使用默认值。
我不希望开发人员认为他们可以或应该提供last_log_time 来运行update。此属性应显示在响应消息中,但不会发送到 api。
如何自定义每个函数的请求格式示例?理想情况下,我会在 doc 块中声明它,如图所示(API 应该只接受 JSON 格式的请求),或者可能根据 Job-class 的属性的注释。
How can we hide a property in WebAPI? 此处提供的答案无济于事,因为如上所述,应在响应中提供last_log_time。如果我用 [IgnoreDataMember] 注释它,它将被全局忽略。
【问题讨论】:
-
@gpro 解释了为什么不是
标签: c# asp.net-web-api documentation