代码之家  ›  专栏  ›  技术社区  ›  San Jaisy

swagger 4.0 asp.net core 2.2需要body参数

  •  0
  • San Jaisy  · 技术社区  · 7 年前

    我使用的是带有asp.net core 2.2web api的swagger 4.0。我想告诉post/put方法上的swagger ui,需要一些body内容参数。如我所知,如果参数是查询参数,那么它是必需的。做了一些研究 https://swagger.io/docs/specification/2-0/describing-request-body/ 并发现可以将正文内容

    swagger document

    其他环节的一些研究

    How to mark a property as required in Swagger, without ASP.NET model validation?

    How can I tell Swashbuckle that the body content is required?

    控制器代码

    [HttpPost]
            public async Task<IActionResult> Create([FromBody]LearningApplication model)
            {
                if (!ModelState.IsValid) return InvalidModelState(ModelState);
    
                // create the record,no need to provide resource auth as only admin can action this controller
                await _learningApplicationManager.CreateAsync(model, LearningApplicationValidator.OnCreateRuleset);
    
                return Created(url, model);
            }
    

    模型

    public class LearningApplication : BaseAuditableWithLogicalDeleteAggregate<Guid>
        {
    
            [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter the name of the course or professional Learning session")]
            [DataMember]
            public string CourseName { get; set; }
    
            [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter the professional Learning provider: ie. TGS, AGSV, etc")]
            [DataMember]
            public string Provider { get; set; }
    
            [DataMember]
            public string EventWebsite { get; set; }
    
            [DataMember]
            public string RegistrationExpenses { get; set; }
    
            [Required(ErrorMessage = "Please provide the Start Date & time")]
            [DataMember]
            public DateTime? StartDateTime { get; set; }
    }
    

    当前摆幅显示

    current swagger

    0 回复  |  直到 7 年前