(七)Action 的返回值 ActionResult

1. 当返回一个页面,return View();  View() 方法的返回值是 ViewResult 类型,继承自 ActionResult。

2. 当重定向一个页面, return Redirect("Path"); Redirect() 方法的返回值类型是 RedirectResult。

3. 直接返回文本内容:ruturn Content("value"); Content() 方法的返回值类型是 ContentResult。

4. 当返回一个文件的时候,使用 File() 方法,File 方法有多个重载:

//返回 byte[] 类型的数据
protected internal FileContentResult File(byte[] fileContents, string contentType);

//返回 Stream 类型的数据
protected internal FileStreamResult File(Stream fileStream, string contentType);

//fileName 为本地文件路径
protected internal FilePathResult File(string fileName, string contentType);

//上面三个方法是在网页直接显示内容
//下面三个方法是对应的直接下载的
protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);

protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);

virtual FilePathResult File(string fileName, string contentType, string fileDownloadName);

5. 当没有返回相应的内容或页面的时候,return HttpNotFound(); 浏览器会给出 404 错误。

6. 也可以返回 JavaScript 代码, 但是返回的是代码字符串,效果和 return Content() 是一样的,尽量不要用。

7. 返回 Josn: return Json(object data), 把 data 对象序列化成 json 字符串给客户端,并且设置 contentType为 “application/json"。Json 方法默认是禁止 Get 请求的,只能 Post 请求,所以如果 Get 请求方法访问是会报错的,如果确实需要使用 Get 方式,使用 return Json(data, JsonRequestBehavior.AllowGet)。



ASP.NET MVC 默认提供的 Json 方法实现有以下缺点:

  1. 日期类型的属性格式化字符串是 "\/Date(1491921608507)\/" 这样的格式,在客户端要用 JS 代码格式化处理,很麻烦。
  2. Json 字符串中的属性的名字和 C#中的大小写一样,不符合 JS 中 ”小写开头,驼峰命名“的习惯。
  3. 无法处理循环引用的问题(尽管应该避免循环引用)。

处理方法: 不使用默认提供的 Json 方法,使用 Json.Net。

时间: 2024-08-02 02:51:03

(七)Action 的返回值 ActionResult的相关文章

Jquery ajax不刷新页面提交action取得返回值

页面采用回调函数function(data) 处理后台返回的结果 a标签onclick事件触发 <a  href ="javascript:void(0);" class="btn btn-default"  id ="add" onclick = "return addproduct(${s.id});">加入秒杀</a> 前台 function addproduct(id){   var mpric

SpringMVC 拦截返回值,并自定义

有关取代mvc:annotation-driven使用自定义配置请看: http://blog.csdn.net/cml_blog/article/details/45222431 1.在项目开发中,自定义全局返回值拦截是非常实用的,就如在Struts2的拦截器中,可以根据Action的返回值自定义返回信息,如果返回SUCCESS就统一返回一个成功的json对象,如果FAIL就返回全局的定义信息 2.配置xml: <context:component-scan base-package="

ActionResult的其它返回值

我们上边所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件.而它的返回类型是ActionResult如 public ActionResult Index() { return View(); } 除了View()之外那我们这里还能用于返回什么值呢? 一.ascx页面 场景:要返回代码片断,比如Ajax返回一个子页 我们先新建一个Action public ActionResult Ascx() { return PartialView(); }

Asp.net MVC 中Controller返回值类型ActionResult

内容转自 http://blog.csdn.net/pasic/article/details/7110134 Asp.net MVC中Controller返回值类型 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonActionAttribute特性的(NoAction) 不能被重载 必须返回ActionResult类型 如: [csharp] view pl

EF5+MVC4系列(8) ActionResult的返回值

我们在MVC的代码中,经常会看到这样的一个 代码 可能有人会有疑问,既然我定义的是ActionResult,为什么返回值会是View方法呢? 其实这个View方法的返回值的类型是ActionResult的子类ViewResult   我们查看MVC源码可以看到 ActionResult是个抽象类,  ActionResult 有多个派生类(子类),有的子类又有子类 (例如 ViewResultBase 就有  ViewResult子类 和  PartialViewResult 这2种子类) 每个

JsonResult作为Action返回值时的错误

JsonResult作为Action返回值时的错误 System.InvalidOperationException: This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to

struts2在配置文件中调用Action的方法返回值

struts2在配置文件中可以调用Action的方法返回值 1.Action中 //文件下载名 public String getDownloadFileName(){ String downloadFileName = ""; String filename = fileName + ".xls"; try { downloadFileName = URLEncoder.encode(filename,"UTF-8"); } catch (Un

ASP.NET MVC ActionResult的其它返回值

一.ascx页面 场景:要返回代码片断,比如Ajax返回一个子页 我们先新建一个Action public ActionResult Ascx() { return PartialView(); } 我们下面再建一个View,仍然是在Action中点右键,AddView.  注意图中勾选. 于是新建了一个ascx页,我们将之少做改写一下 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&

SpringMVC学习(七)——Controller类的方法返回值

本文所有案例代码的编写均建立在前文SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解的案例基础之上,因此希望读者能仔细阅读这篇文章. 返回ModelAndView Controller类方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view.之前我就已讲过,在此并不过多赘述. 返回void 在Controller类方法形参上可以定义request和response,使用request或response指定响应结果: