日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

mvc的Controller返回值类型ActionResult详解

發布時間:2023/12/1 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mvc的Controller返回值类型ActionResult详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、簡介

ActionResult?

操作方法通過執行工作并返回操作結果來響應用戶輸入。?操作結果表示框架將代表操作方法執行的命令。?ActionResult?類是操作結果的基類。

以下類型從?ActionResult?派生:

  • ContentResult

  • EmptyResult

  • FileResult

  • HttpUnauthorizedResult

  • JavaScriptResult

  • JsonResult

  • RedirectResult

  • RedirectToRouteResult

  • ViewResultBase

  地址:https://msdn.microsoft.com/zh-cn/library/system.web.mvc.actionresult.aspx?f=255&MSPPError=-2147217396

  我們預覽下控制器的基類Controller

  

簡單示例

?javascript

<script type="text/javascript"> $(function () {$("#ControlId").change(function () {//ControlId為下拉控件IDvar value = $(this).val();//獲取到當前選中的值if (value != null) {    //Ajax獲取艙位,并設置復選框$.post(          //POST$("#getUrl").val(),  //url{ fmid: value },    //datafunction (data) {   //success:document.getElementById("id_ccl").innerHTML = "";var strHtml = "";var vIsShared = "";for (var key in data) {for (var result in data[key]) {switch (result.toString()) {case "IsShared":{vIsShared = data[key][result];break;}default: break;}}strHtml += "<input type=\"checkbox\" id=\"chk\" " + (vIsShared == true ? " checked=\"checked\" " : " ") + " name=\"chk\" value=\"true\" />";}document.getElementById("id_ccl").innerHTML = strHtml;}, "json" );  //type }else alert(value);});}); </script>
MVC Controllor:
[HttpPost] public ActionResult GetList(int id) {return Json(eccList); }[HttpGet] public ActionResult GetList(int id) {return Json(eccList, JsonRequestBehavior.AllowGet); }

在MVC下,由于對數據的保護,默認情況下request為post,使用GET請求會被阻止。

如果客戶端使用get請求,需要設置behavior為JsonRequestBehavior.AllowGet 。

二、IHttpActionResult

1、Json<T>(T content)

return Json<List<ORDER>>(lstRes);

2、Ok()、?Ok<T>(T content)

return Ok(); return Ok<string>(name);

3、NotFound()

return NotFound();
當需要向客戶端返回找不到記錄時,有時需要用到NotFound()方法 NotFound()方法會返回一個404的錯誤到客戶端。

4、其他

其他還有一些方法,都有它特定的用途。在此貼出來。

4.1、Content<T>(HttpStatusCode statusCode, T value)

[HttpGet]public IHttpActionResult GetContentResult(){return Content<string>(HttpStatusCode.OK, "OK");}

向客戶端返回值和http狀態碼。

4.2、BadRequest()

[HttpGet]public IHttpActionResult GetBadRequest(ORDER order){if (string.IsNullOrEmpty(order.ID))return BadRequest();return Ok();}

向客戶端返回400的http錯誤。

4.3、Redirect(string location)

[HttpGet]public IHttpActionResult RedirectResult(){return Redirect("http://localhost:21528/api/Order/GetContentResult");}

將請求重定向到其他地方。

?

相關:?MVC控制器總結

?

參考:http://www.cnblogs.com/zfdcp-028/p/5788649.html


轉載于:https://www.cnblogs.com/xcsn/archive/2013/01/03/2843115.html

總結

以上是生活随笔為你收集整理的mvc的Controller返回值类型ActionResult详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。