【问题标题】:How to show custom request examples in the .net api documentation?如何在 .net api 文档中显示自定义请求示例?
【发布时间】: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] 注释它,它将被全局忽略。

【问题讨论】:

标签: c# asp.net-web-api documentation


【解决方案1】:

您可以将[ApiExplorerSettings(IgnoreApi = true)] 添加到您的 last_log_time 属性中,但它只会在正文参数中隐藏 last_log_time。

如果你想隐藏在样本格式中,你需要在文件Areas\HelpPage\SampleGeneration\HelpPageSampleGenerator.cs中自定义方法WriteSampleObjectUsingFormatter的源代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2018-05-28
    相关资源
    最近更新 更多