一、簡介
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()
向客戶端返回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