Result(ActionResult、JsonResult、JavaScriptResult等)

一丶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);
        } 
作者:chenze
出处:https://www.cnblogs.com/chenze-Index/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
原文地址:https://www.cnblogs.com/chenze-Index/p/9293860.html