当我 return 键入 HttpResponseMessage 时,示例值和模型在 swagger ui 中为空
When i return type HttpResponseMessage the example value and model is empty in swagger ui
我使用 .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
当我将方法修改为return时,只有产品可以。
[HttpGet]
[ActionName("getProductById")]
public Product GetProductById([FromUri] int id)
{
Product response = service.GetProductById(id);
return response;
}
我如何结合 return HttrResponseMessage 并获得示例值和模型?
您可以通过 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");
}
我使用 .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
当我将方法修改为return时,只有产品可以。
[HttpGet]
[ActionName("getProductById")]
public Product GetProductById([FromUri] int id)
{
Product response = service.GetProductById(id);
return response;
}
我如何结合 return HttrResponseMessage 并获得示例值和模型?
您可以通过 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");
}