Hi,
I am using swagger for my asp.net core 2.2 documentation and for displaying the response model, i have to use[ProducesResponseType(typeof(ResponseObject), 200)].
In my case, i am not directly returning the Response object. I have a static method which takes the output object as paramater and constructs the ResponseObject. Details Below,
//Model.cs Public class UserResponse { public int UserId { get; set; } public string UserMessage{ get; set; } } //Controller.cs [Consumes("application/json")] [ProducesResponseType(typeof(APIResponse), 200)] public IActionResult GetUser(string UserName) { UserResponse userResponse = dbService.GetUser(UserName); if(userResponse.UserId > 0) { return Ok(Utility.ConstructResponse(userResponse , Utility.ConstructMessage(1000, "Success")); } }
public static class Utility { public static APIResponse ConstructResponse(object objResult,Message message) { APIResponse baseResponse = new APIResponse(); TResponse tResponse = new TResponse(System.Net.HttpStatusCode.OK); tResponse.TMessage = message; tResponse.Result = objResult; baseResponse.TResponse = tResponse; return baseResponse; } public static Message ConstructMessage(short Code, string Description) { return new Message { Code = Code, Description = Description }; } }
So,if i use [ProducesResponseType(typeof(APIResponse), 200)] i am getting the model on the swagger when i lunch the api documentation, The Result object is empty as follows
{"TResponse": {"Version": "string","StatusCode": "Continue","Result": {},"TMessage": {"Code": 0,"Description": "string" } } }
<div> </div> <div>Expected Sample output Response Should be </div>
{"TResponse": {"Version": "string","StatusCode": "Continue","Result": {"UserId": 0,"UserMessage": "string" },"TMessage": {"Code": 0,"Description": "string" } } }
I am not sure how to pass the method in typeof(). Any suggestion how to make the url response like this in the swagger documentation. Any help will be highly appreciated.