控制器-各种ActionResult【2】
来源:互联网 发布:成都真那么好么知乎 编辑:程序博客网 时间:2024/06/05 13:16
控制器-各种ActionResult
ASP.NET MVC几种ActionResult的本质
我们所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件。而它的返回类型是ActionResult如
public ActionResult Index() { return View(); }
除了View()之外那我们这里还能用于返回什么值呢?
<1>ascx页面
场景:要返回代码片断,比如Ajax返回一个子页
让我们先建立一个TestController.cs控制器;我们先新建一个Action
public ActionResult Ascx() { return PartialView(); }
然后给这个Action添加一个视图Ascx.cshtml
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Ascx</title></head><body> <div> <table border="1"> <tr><th>中国</th><th>美国</th><th>英国</th></tr> <tr><th>湖南</th><th>纽约</th><th>巴黎</th></tr> </table> </div></body></html>在浏览器中运行 http://localhost:8439/Test/Ascx 得到的结果是一个ascx页面
<2>返回文本
在Test控制器下添加一个Action方法
public ActionResult Text(){ return Content("这是一段文本"); //往前台输出一个字符串//它相当于以下两句代码://Response.write("这是一段文本");//Response.End(); //End 方法使 Web 服务器停止处理脚本并返回当前结果。文件中剩余的内容将不被处理。如果 Response.Buffer 已设置为 TRUE,则调用 Response.End 将缓冲输出}
这个连视图都不用添加
直接在浏览器中运行 http://localhost:8439/Test/Text 结果是在网页上输出“这是一段文本”这么一行字
<3.1>返回Json
有时我们在调用Ajax时还会要求返回对象为Json序列化的结果,如:
public ActionResult ShowJson() { var userinfo = new { Name = "奥巴马", Age = 56 }; return Json(userinfo, JsonRequestBehavior.AllowGet); //var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" }; //return Json(tempObj,JsonRequestBehavior.AllowGet); //JsonRequestBehavior.AllowGet表示:允许来自客户端的 HTTP GET 请求 //return Json(User, JsonRequestBehavior.AllowGet); }在浏览器中运行 http://localhost:8439/Test/ShowJson 然后就弹出一个窗体让你保存ShowJson.json文件。
保存图片后,用记事本打开文件,文件的内容是 :{"Name":"奥巴马","Age":56}
<3.2>返回Json
/// <summary>/// 返回一个JSON数据/// </summary>/// <param name="pageIndex">当前页码</param>/// <returns>JSON数据</returns>public JsonResult Paging(int pageIndex){ //从数据库查询数据 var query = ((from a in db.T_User where a.Id > pageIndex * 5 select a).Take(5)).ToList(); JsonResult json = new JsonResult(); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; //允许来自客户端的HTTP GET请求 json.Data = new { result = query, //将从数据库中查询出来的数据赋值给jsond对象。 }; return json;}
<4>输出JS文件
大多时候js文件都是静态的,但有时js文件可能也要动态生成这时我们可以这样输出:
public ActionResult Js() { return JavaScript("var x=0;"); }在浏览器中运行:http://localhost:8439/Test/Js 弹出一个窗体:如下
保存后,用记事本打开Js.js文件 内容是: var x=0;
<5>页面跳转
1.跳转到Action
public ActionResult rdaction() { return RedirectToAction("Text", "Test");//跳转到Test控制下的Text方法 }
在浏览器中运行:http://localhost:8439/Test/rdaction 于是立刻就跳转到 http://localhost:8439/Test/Text页面了,于是输出
2.跳转到Url
public ActionResult rdurl() { return Redirect("http://www.baidu.com"); }在浏览器中运行:http://localhost:8439/Test/rdurl 于是立刻就跳转到百度的首页了。
3.跳转到Routing规则
public ActionResult rdrouting() //跳转到Routing规则 { return RedirectToRoute("Default",//Route名 new { Controller = "Test", Action = "Ascx" }); }
在浏览器中运行:http://localhost:8439/Test/rdrouting 于是立刻就跳转到 Test控制器下的Ascx方法了,即跳转到http://localhost:8439/Test/Ascx 页面了
于是就输出 http://localhost:8439/Test/Ascx页面的内容
<6>显示文件
public ActionResult fn() { return File("d:/123.jpg","jpg/png/JPEG"); }
在浏览器中运行:http://localhost:8439/Test/fn 弹出一个对话框:如下
点击保存后,选择用图片查看器打开。显示出了 D盘下的123.jpg图片
<7> 返回一个空的页面
public ActionResult EmptyResultDemo() { //仅仅用来占位,没有任何的逻辑。与return null是一样的 return new EmptyResult(); //空方法 }
在浏览器中运行 : http://localhost:8439/Test/EmptyResultDemo 得到的是一个空空如也的空白页
<8>输出流文件
public ActionResult FileStreamResultDemo()//将d盘下的haha.jpg文件转化成一个文件流,返回到前台 { FileStream fs = new FileStream("d:/haha.jpg", FileMode.Open, FileAccess.Read); return File(fs, @"jpg/image/gif");//记得这个File()方法有6个重载哦。到时候可以使用以下哦。 }运行浏览器:http://localhost:8439/Test/FileStreamResultDemo 弹出一个对话框
选择保存。保存后,选择用图片查看器打开,就显示了一幅D盘下的 haha.jpg文件
<9> 返回一个响应状态码
//HttpStatusCodeResult()这要是用来防盗链的。 例如:当博客园里有个哥们在博客园里发布了一个帖子,帖子里放了一个<img src="http://www.jd.con/img/1.jpg">的图片。 当用户访问博客园,浏览这个帖子的时候,浏览器解析到有这么个<img src="http://www.jd.con/img/1.jpg">的图片,于是就向京东的这个网站请求这幅图片。此时京东的服务器就做了一个判断,判断你这个请求的上一次请求URL地址是谁? 于是发现这个请求的上一个地址是http://wwww.cnblogs.com/abc.html。这个地址是博客园的地址,所以此时京东的服务就返回一个404的状态码。表示这幅图片不存在(其实是京东不想给你看,如果上一次的请求是从京东本网过来的话,图片就给你看了。) public ActionResult HttpStatusCodeResultDemo() { //动态修改响应状态码。给请求返回一个404的响应 //404:资源找不到 500:服务器错误 304:缓存 302:重定向 return new HttpStatusCodeResult(404); }
当控制器返回404的状态码的时候,执行这个视图的时候,浏览器的反应是
0 0
- 控制器-各种ActionResult【2】
- 控制器-各种ActionResult【1】
- ASP.Net MVC 各种ActionResult
- mvc 控制器中 return ActionResult 的注意
- ActionResult
- ActionResult的实现(2)-mvc3
- MVC里ActionResult或ViewResult跳转到当前控制器下对应的视图里的做法
- ActionResult解析
- ActionResult解析
- spring mvc 各种控制器的区别关系
- 實作ActionResult來讀取二進位圖片並顯示
- ActionResult 的实现(1)
- ActionResult 返回类型
- MVC3 ActionResult 返回类型
- MVC 返回ActionResult 类型
- MVC中的ActionResult
- MVC4笔记之ActionResult
- MVC Action和ActionResult
- ini文件的操作
- Valid Parentheses
- java的FileChannel使用方法。
- 由浅入深谈流形
- 求N个实数的最大值和最小值
- 控制器-各种ActionResult【2】
- nyoj-223-小明的烦恼
- 设计模式(九)之 Decorator(油漆工)
- 2014校园招聘之二(3月阿里巴巴实习生笔试题)
- c语言面试知识总结
- 函数
- SGU Inversions(线段树)
- 构造可配置词法语法分析器生成器(中)
- android开发步步为营之27:上下文菜单ContextMenu的用法