1 private readonly ORDER oRDER1 = new ORDER() { ID = "aa", NO = "10", NAME = "bb", DESC = "0" };
2 private readonly ORDER oRDER2 = new ORDER() { ID = "dd", NO = "20", NAME = "cc", DESC = "1" };
3 //StatusCode:返回带有提供的HTTP码(和一个空响应体)的响应
4 public IHttpActionResult GetStatusCode(){
5 return StatusCode(HttpStatusCode.OK);
6 }
7 //Json:返回HTTP 200(OK),将提供内容格式化为JSON格式,还可以使用提供的序列化器设置和字符编码来格式化内容
8 public IHttpActionResult GetJsonResult(int id){
9 switch (id){
10 case 1: return Json<List<ORDER>>(new List<ORDER> { oRDER1,oRDER2 }, new JsonSerializerSettings(), System.Text.Encoding.UTF8);
11 case 2: return Json<object>(new { A = "aa", B = "bb" }, new JsonSerializerSettings(), System.Text.Encoding.UTF8);
12 default: return Json<dynamic>(new { A = "aa", B = "bb" }, new JsonSerializerSettings(), System.Text.Encoding.UTF8);
13 }
14 }
15 // Ok:返回HTTP 200,可以包括返回的内容
16 public IHttpActionResult GetOKResult(int id){
17 switch (id){
18 case 1: return Ok<string>("aaaa");//简化return Ok("aaaa");
19 case 2: return Ok<ORDER>(oRDER1);//return Ok(oRDER1);
20 default: return Ok();
21 }
22 }
23 //Content:返回HTTP 200,返回内容,内容格式可以选定返回的内容,HTTP状态码,格式
24 public IHttpActionResult GetContent(){
25 return Content(HttpStatusCode.OK, oRDER1, new JsonMediaTypeFormatter(), new MediaTypeHeaderValue("application/json"));
26 }
27 //Created:返回HTTP 201,位置头被设,为提供URL的位置
28 public IHttpActionResult GetCreated(){
29 return Created("ActionApi", new { controller = "TestReturnData", action = "GetBadRequest" });
30 }
31 //CreateAtRoute:返回HTTP 201,位置头被设为基于提供的路由名和路由值构成的URL
32 public IHttpActionResult GetCreatedAtRoute(){
33 return CreatedAtRoute<ORDER>("ActionApi", new { controller = "TestReturnData", action = "GetBadRequest" }, oRDER1);
34 }
35 //Redirect:返回HTTP 302,位置头被设为指定的URL位置,重定向
36 public IHttpActionResult GetRedirectResult(int id){
37 if (id == 1){
38 return Redirect("http://localhost:61218/actionapi/TestReturnData/GetContent");
39 }else{
40 UrlHelper helper = new UrlHelper(Request);
41 IDictionary<string, object> route = new Dictionary<string, object>{
42 ["controller"] = "TestReturnData",["action"] = "GetBadRequest"
43 };
44 return Redirect(new Uri(helper.Link("ActionApi", route)));
45 }
46 }
47 //RedirectToRoute:返回HTTP302,位置头被设为基于提供的路由名和路由值构成的URL,重定向
48 public IHttpActionResult GetRedirectToRoute(){
49 return RedirectToRoute("ActionApi", new { controller = "TestReturnData", action = "GetBadRequest" });
50 }
51 //BadRequest:返回HTTP 400,根据验证错误,可能包含一条信息或者一个自动格式化的错误类
52 public IHttpActionResult GetBadRequest(){
53 return BadRequest("error");//也可以不包括错误信息
54 }
55 //Unauthorized:返回HTTP 401,身份验证头被设为提供的身份验证头值
56 public IHttpActionResult GetUnauthorized(){
57 return Unauthorized(new List<AuthenticationHeaderValue>() { new AuthenticationHeaderValue("Authorization") });
58 }
59 //NotFound:返回HTTP 404 没有找到
60 public IHttpActionResult GetNotFoundResult(){
61 return NotFound();
62 }
63 // Conflict:返回HTTP 409 冲突
64 public IHttpActionResult GetConflict(){
65 return Conflict();
66 }
67 //InternalServerError:返回HTTP 500,可能会包含提供的异常派生出的内容
68 public IHttpActionResult GetInternalServerError(){
69 //return InternalServerError();
70 return InternalServerError(new NotImplementedException());
71 }