BaseController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. using System.Web.Http;
  12. using XdCxRhDw.Dto;
  13. namespace XdCxRhDW.WebApi.Controllers
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. [RoutePrefix("")]
  19. public class BaseController : ApiController
  20. {
  21. /// <summary>
  22. /// 返回成功
  23. /// </summary>
  24. /// <param name="data">返回的数据</param>
  25. /// <returns></returns>
  26. protected AjaxResult Success(object data = null)
  27. {
  28. AjaxResult res = new AjaxResult
  29. {
  30. code = 200,
  31. msg = "请求成功!",
  32. data = data
  33. };
  34. return res;
  35. }
  36. /// <summary>
  37. /// 返回成功
  38. /// </summary>
  39. /// <param name="msg">返回的消息</param>
  40. /// <param name="data">返回的数据</param>
  41. /// <returns></returns>
  42. protected AjaxResult Success(object data, string msg = "请求成功!")
  43. {
  44. AjaxResult res = new AjaxResult
  45. {
  46. code = 200,
  47. msg = msg,
  48. data = data
  49. };
  50. return res;
  51. }
  52. /// <summary>
  53. /// 返回成功
  54. /// </summary>
  55. /// <param name="msg">返回的消息</param>
  56. /// <param name="data">返回的数据</param>
  57. /// <returns></returns>
  58. protected AjaxResult<T> Success<T>(T data, string msg = "请求成功!")
  59. {
  60. AjaxResult<T> res = new AjaxResult<T>
  61. {
  62. code = 200,
  63. msg = msg,
  64. data = data
  65. };
  66. return res;
  67. }
  68. /// <summary>
  69. /// 返回错误
  70. /// </summary>
  71. /// <param name="msg">错误提示</param>
  72. /// <returns></returns>
  73. protected AjaxResult Error(string msg)
  74. {
  75. AjaxResult res = new AjaxResult
  76. {
  77. code = 0,
  78. msg = msg,
  79. data = null
  80. };
  81. return res;
  82. }
  83. /// <summary>
  84. /// 返回错误
  85. /// </summary>
  86. /// <param name="msg">错误提示</param>
  87. /// <returns></returns>
  88. protected AjaxResult<T> Error<T>(string msg)
  89. {
  90. AjaxResult<T> res = new AjaxResult<T>
  91. {
  92. code = 0,
  93. msg = msg,
  94. data = default
  95. };
  96. return res;
  97. }
  98. #region WebAPI 获取传统Context
  99. /// <summary>
  100. /// Context
  101. /// </summary>
  102. public HttpContextBase Context
  103. {
  104. get
  105. {
  106. return (HttpContextBase)Request.Properties["MS_HttpContext"];
  107. }
  108. }
  109. /// <summary>
  110. /// Request
  111. /// </summary>
  112. public HttpRequestBase HttpRequest
  113. {
  114. get
  115. {
  116. return Context.Request;
  117. }
  118. }
  119. /// <summary>
  120. /// Response
  121. /// </summary>
  122. public HttpResponseBase HttpResponse
  123. {
  124. get
  125. {
  126. return Context.Response;
  127. }
  128. }
  129. #endregion
  130. }
  131. }