ValuesController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Http.Features;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Newtonsoft.Json;
  7. using Vit.Core.Util.ComponentModel.Data;
  8. using Vit.Core.Util.ComponentModel.Model;
  9. namespace Did.Serslot.Demo.Controllers
  10. {
  11. [Route("did_serslot/[controller]")]
  12. [Route("did_serslot/v1")]
  13. [ApiController]
  14. public class ValuesController : ControllerBase
  15. {
  16. #region (x.1)route
  17. /// <summary>
  18. /// GET did_serslot/Values
  19. /// </summary>
  20. /// <returns></returns>
  21. [HttpGet]
  22. public object Route0([FromQuery]string a)
  23. {
  24. //var requestFeature = Request.HttpContext.Features.Get<IHttpRequestFeature>();
  25. return "GET did_serslot/Values?a=" + a;
  26. }
  27. /// <summary>
  28. /// GET did_serslot/Values/route_x
  29. /// </summary>
  30. /// <returns></returns>
  31. [HttpGet("101/route")]
  32. [HttpGet("102/route")]
  33. [HttpGet("/did_serslot/Values/103/route")]
  34. public object Route1()
  35. {
  36. return new
  37. {
  38. Request.Path,
  39. Request.Method
  40. };
  41. }
  42. /// <summary>
  43. /// GET did_serslot/Values/route104
  44. /// </summary>
  45. /// <returns></returns>
  46. [HttpGet("[action]")]
  47. public object route104()
  48. {
  49. return new
  50. {
  51. name = "GET did_serslot/Values/route104",
  52. Request.Path,
  53. Request.Method
  54. };
  55. }
  56. #endregion
  57. #region (x.2)Name和Desc
  58. /// <summary>
  59. /// 演示 如何使用Name 和 Description。
  60. /// 函数注释和使用SsDescription是一样的效果。
  61. /// </summary>
  62. /// <returns></returns>
  63. [HttpGet("201/NameDesc")]
  64. [SsName("NameDesc1")]
  65. public ApiReturn NameDesc()
  66. {
  67. return new ApiReturn();
  68. }
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. /// <returns></returns>
  73. [HttpGet("202/NameDesc")]
  74. [SsDescription("演示 如何使用Name 和 Description。\n函数注释和使用SsDescription是一样的效果。")]
  75. public ApiReturn NameDesc2()
  76. {
  77. return new ApiReturn();
  78. }
  79. #endregion
  80. #region (x.3) 参数
  81. #region (x.x.1) 从route获取参数
  82. /// <summary>
  83. /// 从route获取参数
  84. /// GET did_serslot/Values/301/arg/{id}/{id2}
  85. /// </summary>
  86. /// <returns></returns>
  87. [HttpGet("301/arg/{name}/{age}")]
  88. public object Arg301(
  89. [SsExample("lith"), SsDefaultValue("NoName"), SsDescription("姓名")]string name,
  90. [SsExample("30"), SsDefaultValue("0"), SsDescription("年龄,请指定在16-50中间的整数")]int age)
  91. {
  92. return new
  93. {
  94. Request.Path,
  95. Request.Method,
  96. request = new { name, age }
  97. };
  98. }
  99. #endregion
  100. #region (x.x.2)从Query String获取参数
  101. /// <summary>
  102. /// 从Query String获取参数
  103. /// GET did_serslot/Values/302/arg?name=lith&amp;age=30
  104. /// </summary>
  105. /// <returns></returns>
  106. [HttpGet("302/arg")]
  107. public object Arg302(
  108. [FromQuery, SsExample("lith"),SsDescription("姓名")]string name,
  109. [FromQuery, SsExample("30"), SsDescription("年龄,请指定在16-50中间的整数")]int age)
  110. {
  111. return new
  112. {
  113. Request.Path,
  114. Request.Method,
  115. request = new { name, age }
  116. };
  117. }
  118. #endregion
  119. #region (x.x.3)从Body获取参数
  120. /// <summary>
  121. /// 从Body获取参数
  122. /// POST did_serslot/Values/303/arg
  123. /// </summary>
  124. /// <returns></returns>
  125. [HttpPost("303/arg")]
  126. public object Arg3([FromBody, SsExample("\"lith\""), SsDescription("姓名,请以双引号开始和结束")]string name)
  127. {
  128. return new
  129. {
  130. Request.Path,
  131. Request.Method,
  132. request = new { name }
  133. };
  134. }
  135. #endregion
  136. #region (x.x.4)从Body获取参数
  137. /// <summary>
  138. /// 从Body获取参数
  139. /// POST did_serslot/Values/304/arg
  140. /// </summary>
  141. /// <returns></returns>
  142. [HttpPost("304/arg")]
  143. public object Arg4([FromBody] ArgModel arg)
  144. {
  145. return new
  146. {
  147. Request.Path,
  148. Request.Method,
  149. request = arg
  150. };
  151. }
  152. public class ArgModel
  153. {
  154. /// <summary>
  155. /// 姓名
  156. /// </summary>
  157. [SsExample("lith"), SsDefaultValue("NoName")]
  158. [JsonProperty("arg")]
  159. public string name { get; set; }
  160. /// <summary>
  161. ///
  162. /// </summary>
  163. [SsExample("20"), SsDefaultValue("0"), SsDescription("年龄,请指定在16-50中间的整数")]
  164. public int age;
  165. }
  166. #endregion
  167. #endregion
  168. #region (x.4) 返回值
  169. #region (x.x.1)返回值注释
  170. /// <summary>
  171. ///
  172. /// </summary>
  173. /// <returns></returns>
  174. [HttpGet("401/ret")]
  175. [return: SsExample("test1"), SsDescription("返回test1")]
  176. public string Return4()
  177. {
  178. return "test1";
  179. }
  180. /// <summary>
  181. ///
  182. /// </summary>
  183. /// <returns></returns>
  184. [HttpGet("402/ret")]
  185. [return: SsExample("5"), SsDescription("返回5")]
  186. public int Return5()
  187. {
  188. return 5;
  189. }
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. /// <returns></returns>
  194. [HttpGet("403/ret")]
  195. [return: /*SsExample("{\"name\":\"张三\"}"),*/ SsDescription("返回模型数据")]
  196. public ReturnData Return6()
  197. {
  198. return new ReturnData { name = "张三" };
  199. }
  200. /// <summary>
  201. /// Return注释-FromType
  202. /// </summary>
  203. public class ReturnData
  204. {
  205. /// <summary>
  206. /// 姓名
  207. /// </summary>
  208. [SsExample("lith"), SsDefaultValue("NoName")]
  209. public string name { get; set; }
  210. }
  211. #endregion
  212. #region (x.x.2)异步返回
  213. /// <summary>
  214. /// GET did_serslot/Values/201/result
  215. /// </summary>
  216. /// <returns></returns>
  217. [HttpGet("420/result")]
  218. public object Result1()
  219. {
  220. return Request.Method + " " + Request.Path;
  221. }
  222. /// <summary>
  223. /// GET did_serslot/Values/202/result
  224. /// </summary>
  225. /// <returns></returns>
  226. [HttpGet("421/result")]
  227. public ActionResult<string> Result2()
  228. {
  229. return Request.Method + " " + Request.Path;
  230. }
  231. /// <summary>
  232. /// GET did_serslot/Values/203/result
  233. /// </summary>
  234. /// <returns></returns>
  235. [HttpGet("422/result")]
  236. public ActionResult<IEnumerable<string>> Result3()
  237. {
  238. return new string[] { Request.Method + " " + Request.Path, "" };
  239. }
  240. /// <summary>
  241. /// GET did_serslot/Values/204/result
  242. /// </summary>
  243. /// <returns></returns>
  244. [HttpGet("423/result")]
  245. public async Task<string> Result4()
  246. {
  247. await Task.Run(() => { Thread.Sleep(2000); });
  248. return Request.Method + " " + Request.Path;
  249. }
  250. /// <summary>
  251. /// GET did_serslot/Values/205/result
  252. /// </summary>
  253. /// <returns></returns>
  254. [HttpGet("424/result")]
  255. public async Task<ActionResult<string>> Result5()
  256. {
  257. await Task.Run(() => { Thread.Sleep(2000); });
  258. return Request.Method + " " + Request.Path;
  259. }
  260. #endregion
  261. #endregion
  262. #region (x.5) HttpMethod
  263. /// <summary>
  264. /// GET did_serslot/Values/500/method
  265. /// </summary>
  266. /// <returns></returns>
  267. [HttpGet("500/method")]
  268. public string Method_Get()
  269. {
  270. return Request.Method + " " + Request.Path;
  271. }
  272. /// <summary>
  273. /// POST did_serslot/Values/500/method
  274. /// </summary>
  275. /// <returns></returns>
  276. [HttpPost("500/method")]
  277. public string Method_Post()
  278. {
  279. return Request.Method + " " + Request.Path;
  280. }
  281. /// <summary>
  282. /// Put did_serslot/Values/500/method
  283. /// </summary>
  284. /// <returns></returns>
  285. [HttpPut("500/method")]
  286. public string Method_Put()
  287. {
  288. return Request.Method + " " + Request.Path;
  289. }
  290. /// <summary>
  291. /// Delete did_serslot/Values/500/method
  292. /// </summary>
  293. /// <returns></returns>
  294. [HttpDelete("500/method")]
  295. public string Method_Delete()
  296. {
  297. return Request.Method + " " + Request.Path;
  298. }
  299. ///// <summary>
  300. ///// Head did_serslot/Values/500/method
  301. ///// </summary>
  302. ///// <returns></returns>
  303. //[HttpHead("500/method")]
  304. //public string Method_Head()
  305. //{
  306. // return Request.Method + " " + Request.Path;
  307. //}
  308. /// <summary>
  309. /// Options did_serslot/Values/500/method
  310. /// </summary>
  311. /// <returns></returns>
  312. [HttpOptions("500/method")]
  313. public string Method_Options()
  314. {
  315. return Request.Method + " " + Request.Path;
  316. }
  317. /// <summary>
  318. /// Patch did_serslot/Values/500/method
  319. /// </summary>
  320. /// <returns></returns>
  321. [HttpPatch("500/method")]
  322. public string Method_Patch()
  323. {
  324. return Request.Method + " " + Request.Path;
  325. }
  326. /// <summary>
  327. /// get|post did_serslot/Values/501/method
  328. /// </summary>
  329. /// <returns></returns>
  330. [Route("501/method")]
  331. [HttpGet, HttpPost]
  332. public string Method2()
  333. {
  334. return Request.Method + " " + Request.Path;
  335. }
  336. #endregion
  337. }
  338. }