一丶ActionResult
應用於Action方法前面的類型,它是Action的返回值,代表Action的執行結果。
public ActionResult Index() { return View(); }
二丶JsonResult
返回Json字符串,但ajax接受到后,會自動轉換成Json對象進行使用。
public ActionResult Val1() { //生成3個隨機數 Random rd = new Random(); var a = rd.Next(1, 1000); var b = rd.Next(10, 1000); var c = rd.Next(100, 1000); //申明一個匿名類 var msg = new { a1 = a, a2 = b, a3 = c, }; return Json(msg); }
三丶JavaScriptResult
通過后台返給前端js代碼。
1 public ActionResult GetJs() 2 { 3 return JavaScript("alert('我是js代碼,調用的是JavaScriptResult')"); 4 } 5 public ActionResult GetJs2() 6 { 7 return Content("alert('我是js代碼,調用的是ContentResult,並自定義的返回類型為js')", "application/x-javascript"); 8 }
四丶FileResult
應用於下載文件
public FileResult DownLoadExcel(StudentInfo studentInfo, Student_Photo student_Photo, string dateStart, string dateEnd) { List<StuInfoAndImg> stuInfoAndImgList = GetStu_List(studentInfo, student_Photo, dateStart, dateEnd); string pathFileName = string.Empty; ExportStudentExcel(stuInfoAndImgList, out pathFileName); string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".xlsx"; return File(pathFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); }