我使用.net框架和nuget包Swashbuckle。 我有方法
的控制器 [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");
}
SwaggerConfig是
[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 =>
{
});
}
}
}
但是现在当我运行项目和url localhost:61342 / swagger / ui / index时,我遇到的问题是示例值和模型为空。 https://prnt.sc/o7wlqe
当我修改仅返回产品的方法时就可以了。
[HttpGet]
[ActionName("getProductById")]
public Product GetProductById([FromUri] int id)
{
Product response = service.GetProductById(id);
return response;
}
我如何结合以返回HttrResponseMessage并获得示例值和模型?
答案 0 :(得分:0)
您可以通过ResponseTypeAttribute
属性声明响应类型:
[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");
}