一丶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
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); }