mark:环境看之前文章
目的:web api controller 调用 asp.net mvc controller,让swagger里面的返回模型支持注释
关键:对返回消息类的封装,返回数据为泛型,swagger就能显示model的注释了
JsonMsg
/// <summary>
/// 返回消息
/// </summary>
public class JsonMsg<T> where T : class
{
/// <summary>
/// 状态码
/// </summary>
public int code { get; set; }
/// <summary>
/// 消息
/// </summary>
public string msg { get; set; }
/// <summary>
/// 内容
/// </summary>
public T obj { get; set; }
/// <summary>
/// 图标
/// </summary>
public int icon { get; set; }
public static JsonMsg<T> OK(T obj, string msg = "成功")
{
return new JsonMsg<T>() { code = 1, msg = msg, obj = obj, icon = 1 };
}
public static JsonMsg<T> Error(T obj, string msg = "失败")
{
return new JsonMsg<T>() { code = 0, msg = msg, obj = obj, icon = 1 };
}
}
OrderDto
public class OrderDto
{
public string Name { get; set; }
}
HomeController
public JsonResult GetOrderInfo()
{
var order = new OrderDto { Name = "203022200" };
return Json(order);
}
HomeAPIController
[AllowAnonymous]
public JsonMsg<OrderDto> testResultDataDesc3()
{
HomeController controller = GetController<HomeController>();
var d = (OrderDto)controller.GetOrderInfo().Data;
return JsonMsg<OrderDto>.OK(d);
}