Reputation: 2339
I use .net framework and the nuget package Swashbuckle. I have the controller with the method
[HttpGet]
[ActionName("getProductById")]
public HttpResponseMessage GetProductById([FromUri] int id)
{
Product response = service.GetProductById(id);
if (response != null)
{
return Request.CreateResponse<Product>(HttpStatusCode.OK, response);
}
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
}
The SwaggerConfig is
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace ProductsApp
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "ProductsApp");
})
.EnableSwaggerUi(c =>
{
});
}
}
}
But now when I run the project and the url localhost:61342/swagger/ui/index i have the problem that example values and model is empty. https://prnt.sc/o7wlqe
When i modify the method to return only the product is ok.
[HttpGet]
[ActionName("getProductById")]
public Product GetProductById([FromUri] int id)
{
Product response = service.GetProductById(id);
return response;
}
How i can combine to return the HttrResponseMessage and to have the example values and model?
Upvotes: 7
Views: 1571
Reputation: 4048
You can declare the response type via the ResponseTypeAttribute
attribute:
[HttpGet]
[ActionName("getProductById")]
[ResponseType(typeof(Product))]
public HttpResponseMessage GetProductById([FromUri] int id)
{
Product response = service.GetProductById(id);
if (response != null)
{
return Request.CreateResponse<Product>(HttpStatusCode.OK, response);
}
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
}
Upvotes: 8