Result(ActionResult、JsonResult、JavaScriptResult等)

前端之家收集整理的这篇文章主要介绍了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);
        } 

猜你在找的Json相关文章