Reputation: 59
I have the following like this below format for Example Value at Swagger UI:
{
"Username": "string",
"Interest": "string",
"Position": "string"
}
And here is the following for the Response Body at Swagger UI:
{
"ErrorCode": "0",
"ErrorMessage": "Success.",
"Result": [
{
"Username": "Test",
"Interest": "Test",
"Position": "Test"
}
]
}
My question is, how can I make the Example Value at Swagger UI to be like the Response Body at Swagger UI?
Here is the code that I am using:
Model class:
public class ActionResultExtended
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
public object Result { get; set; }
}
public class User
{
public string Username { get; set; }
public string Interest { get; set; }
public string Position { get; set; }
}
Controller class:
[ResponseType(typeof(User))] // This is where the Example Value comes from
public HttpResponseMessage GetUser()
{
var userModel = new User
{
Username = "Test",
Interest = "Test",
Position = "Test"
};
var response = new ActionResultExtended()
{
ErrorCode = "0",
ErrorMessage = "Success.",
Result = userModel
};
return Request.CreateResponse(HttpStatusCode.OK, response);
}
I have tried the following, but it couldn't work as expected.
How can I extend the ResponseType attribute from Web API? So that I can make it like the Response Body instead of the Example Value at Swagger UI.
Any help would be appreciated. Thanks
Upvotes: 0
Views: 835
Reputation: 59
I have solved it by modifying my model class like this:
Model class:
public class ActionResultExtended
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
public User Result { get; set; }
}
public class User
{
public string Username { get; set; }
public string Interest { get; set; }
public string Position { get; set; }
}
And from the ResponseType attribute set it like this:
[ResponseType(typeof(ActionResultExtended))]
This way, the Example Value in Swagger UI will be same like Response Body in Swagger UI:
{
"ErrorCode": "0",
"ErrorMessage": "Success.",
"Result": [
{
"Username": "Test",
"Interest": "Test",
"Position": "Test"
}
]
}
Thank you.
Upvotes: 1