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