ASP.NET 控制器

來源:互聯網
上載者:User

標籤:rip   使用   att   匿名對象   tar   模型   phone   ++   edit   

1.繼承Controller
 public class TestController : Controller


2.編寫控制器方法
    // URL  :   test/Edit/1
        [HttpPost]//http請求方式
        public ActionResult Edit(int id, FormCollection collection)
        {
    //public (必須傳回型別) 名稱  (參數類型 參數,FormCollection:擷取表單資料)  
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

3.傳回值類型:

在mvc中所有的controller類都必須使用"Controller"尾碼來命名 並且對Action也有一定的要求:

    必須是一個public方法
    必須是執行個體方法
    沒有標誌NonActionAttribute特性的(NoAction)
    不能被重載



Asp.net MVC中Controller傳回值類型:

    ActionResult:返回視圖,model對象()
    ViewResult:返回視圖
    ContentResult:返回字串(可以用於json 字串)
    JsonResult:返回JsonResult類型(返回json)
    RedirectToRouteResult:控制器跳轉控制器




參考資料:http://blog.csdn.net/pasic/article/details/7110134

 

 

 

ViewResult

傳回值為ViewResult即返回展示給使用者的前台頁面(視圖),return View()可以返回本控制器內的任意視圖。當return View()返回與Action不同名的視圖時會直接展示視圖,而不是執行對應的Action方法。

public ViewResult Index(){    UserInfoModel userInfoModel = new UserInfoModel();    userInfoModel.AddTime = DateTime.Now;    userInfoModel.UserName = "使用者名稱";    int number = 123; //【重載1】預設返回與Action同名的視圖 return View(); //【重載2】返回本控制器下的Add視圖 return View("Add"); //【重載3】一般傳遞的參數對象的類型與視圖的模型類一致 return View("Add", userInfoModel);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

 

ContentResult

ContentResult用於將字串直接向用戶端輸出。
後台代碼:

public ContentResult GetUserInfo(){    string userInfo = "[{\"UserName\":\"蟈蟈\",\"Age\":\"18\",\"PhoneNumber\":\"18233199999\"}," + "{\"UserName\":\"果果\",\"Age\":\"16\",\"PhoneNumber\":\"18233199999\"}," + "{\"UserName\":\"郭郭\",\"Age\":\"20\",\"PhoneNumber\":\"18233199999\"}]"; return Content(userInfo);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

 

前台代碼:

function TestActionResult_Index_AddUserInfo1(userInfo) {    //先把字串轉換為Json對象 var userInfoArray = JSON.parse(userInfo); for (var i = 0; i < userInfoArray.length; i++) { $("<tr></tr>").append("<td>" + userInfoArray[i].UserName + "</td>") .append("<td>" + userInfoArray[i].Age + "</td>") .append("<td>" + userInfoArray[i].PhoneNumber + "</td>") .appendTo($("#TestActionResult_Index_tb")); }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

 

JsonResult

JsonResult首先將指定的對象序列化為Json字串,然後將字串寫入到HTTP輸出資料流。由於Json字串在MVC前後台互動過程中使用頻率很高,所以多舉幾個例子。
1)返回Json數組

public JsonResult GetUserInfoJsonArray(){    List<object> userInfoList = new List<object>(); //userInfoList添加匿名對象 userInfoList.Add(new { UserName="蟈蟈",Age=18,PhoneNumber="18233199999" }); userInfoList.Add(new { UserName="果果",Age=16,PhoneNumber="18233199999" }); userInfoList.Add(new { UserName="郭郭",Age=20,PhoneNumber="18233199999" }); //如果請求方式為get,則必須設定JsonRequestBehavior.AllowGet return Json(userInfoList, JsonRequestBehavior.AllowGet);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1

2)返回Json對象

public JsonResult GetUserInfoJsonObject(){    List<object> userInfoList = new List<object>(); userInfoList.Add(new { UserName = "蟈蟈",Age = 18,PhoneNumber = "18233199999" }); userInfoList.Add(new { UserName = "果果",Age = 16,PhoneNumber = "18233199999" }); userInfoList.Add(new { UserName = "郭郭",Age = 20,PhoneNumber = "18233199999" }); //【方法1】返回匿名對象或執行個體對象 var resultList = new { Company = "熱血傳奇", President = "蟈蟈", UserInfoList = userInfoList }; return Json(resultList, JsonRequestBehavior.AllowGet); //【方法2】返回Dictionary對象 Dictionary<string, object> dict = new Dictionary<string, object>() { {"Company","熱血傳奇"}, {"President","蟈蟈"}, {"UserInfoList",userInfoList} }; return Json(dict, JsonRequestBehavior.AllowGet);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 2
RedirectToRouteResult

RedirectToRouteResult可以跳轉到本項目內任意控制器的Action,當重新導向路由時會根據參數執行對應的控制器下的Action方法,而不是直接展示頁面。

public ViewResult Index(){    UserInfoModel userInfoModel = new UserInfoModel();    userInfoModel.AddTime = DateTime.Now;    userInfoModel.UserName = "使用者名稱";    int number = 123; //【重載1】訪問Home控制器下的Add方法 return RedirectToAction("Add", "Home"); //【重載2】訪問Home控制器下的Add方法,並且參數id=6 return RedirectToAction("Add", "Home", new {id = 6});}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

 

ContentResult與JsonResult返回Json資料的區別

1.當返回到前端的json資料,不標準時如–”{\”Age\”:\”abcde\”,\”name\”:\”rer\”}”,前端抓包的值和我給的一樣。不管你的傳回值類型是
JsonResult還是String,都需要使用JSON.parse(Data)手動轉換一下,才能把字串變為JSON對象。
2.當返回到前端的json資料,標準時如–”{“Age”:”abcde”,”name”:”rer”}”,前端抓包的值和我給的一樣。
只需要保證響應報文頭的ContentType = “application/json,JQ 都會自動把JSON字串轉換為JSON對象。

ASP.NET 控制器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.