ASP.NET MVC中ActionResult的不同返回方式

1.返回视图

return View();//返回方法名对应的视图

return View("aaa");//返回名称为aaa的视图

2.返回文本内容

return Content("hello world");

3.返回重定向

return Redirect("https://www.baidu.com");

return Redirect("~/Test/1.html");

return RedirectToAction("同一个controller类下的方法名");

return RedirectToAction("action name","controller name"); //重定向到指定的controller类下的指定的action方法

4.返回文件

return File(byte[] fileContents, string contentType);

return File(Stream fileStream, string contentType);

return File(string fileName, string contentType);

5.返回json

Person p1 = new Person(){Id=1,Name="zhangsan"};

return Json(p1);

Redirect 和 View 的区别

Redirect和服务器进行了两次交互,第一次是F1,第二次是Index,所以第2次已经取不到ViewBag的值了

View是让服务器把指定的cshtml的内容运行渲染后给到浏览器,只有一次交互,可以取到ViewBag的值

注意:两次地址栏都是输入的Test/f1,Redirect重定向到index,而View是把index的内容渲染出来后放在f1页面显示。

猜你喜欢

转载自www.cnblogs.com/1016391912pm/p/12119253.html