Controller中几种Action返回类型对比

  通过学习,我们可以发现,在Controller中提供了很多不同的Action返回类型。
那么具体他们是有什么作用呢?它们的用法和区别是什么呢?通过资料书上的介绍和网上资料的查询,这里就来给大家列举和大致的概括下。

(1). ActionResult(base):最基本的Action类型,返回其他类型都可以写ActionResult。

(2). ContentResult:返回ContentResult用户定义的内容类型。

public ActionResult Content()  
{  
   return Content("Test Content", "text/html"); // 可以指定文本类型  
} 
  页面输出“Test Content”;此类型多用于在ajax操作中需要返回的文本内容。
  还可以添加比如JavaScript脚本(“text/javascript”)和CSS样式(“text/css”)等,可以修改样式和js等。
 
(3). EmptyResult返回Null或者Void数据类型的EmptyResult。
public ActionResult Empty()  
{  
     return null;  
}  

  返回结果为NULL。

(4). HttpUnauthorizedResult:响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页。

public ActionResult HttpUnauthorizedResult()
{
      return new HttpUnauthorizedResult();
}

(5)JavaScriptResult:返回JavaScriptResult可在客户端执行的脚本。

public ActionResult JavaScript()  
{  
    string str = string.Format("alert('{0}');", "弹出窗口");  
    return JavaScript(str);  
}
  但这里并不会直接响应弹出窗口,需要用页面进行再一次调用。
  这个可以方便根据不同逻辑执行不同的js操作。

(6). JsonResult:返回JsonResult序列化的Json对象。

复制代码
public ActionResult Json()  
{  
     Dictionary<string, object> dic = new Dictionary<string, object>();  
     dic.Add("id", 100);  
     dic.Add("name", "hello");  
     return Json(dic, JsonRequestBehavior.AllowGet);  
} 
复制代码
  主要用于返回json格式对象,可以用ajax操作;
  注意:需要设置参数,JsonRequestBehavior.AllowGet。
  否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
  若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

(7). FileResult (base):返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能。

复制代码
public ActionResult File()  
{  
     string fileName = "~/Content/test.zip"; // 文件名  
     string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名  
     return File(fileName, "application/octet-stream", downFileName);  
}  
复制代码

  直接下载test.zip后保存到本地则为"文件显示名称.zip"。

(8). FileContentResult:是针对文件内容创建的FileResult,它只是调用当前HttpResponse的OutputStream属性的Write方法直接将表示文件内容的字节数组写入响应输出流。

(9). FilePathResult:是一个根据物理文件路径创建FileResult。

(10). FileStreamResult:允许我们通过一个用于读取文件内容的流来创建FileResult。

  PS:关于File的几种返回类型的详细介绍,可以参考园友蒋金楠的博文 “了解ASP.NET MVC几种ActionResult的本质:FileResult” ,已经写的很详细了。

(11). RedirectResult直接转到指定的url地址。

public ActionResult Redirect()  
{  
    // 直接返回指定的url地址  
    return Redirect("http://www.cnblogs.com");  
}  

(12). RedirectToRouteResult直接使用 Action Name 进行跳转,也可以加上ControllerName 以及参数。

public ActionResult RedirectResult()  
{  
    return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });  
} 

(13). RedirectToActionResult指定路由进行跳转。

public ActionResult RedirectRouteResult()  
{  
    return RedirectToRoute("Default", new { controller = "Home", action = "Index"});  
} 

  其中,Default为global.asax.cs中定义的路由名称。

(14). ViewResultBase(base)

(15). ViewResult:返回ViewResult视图结果,将视图呈现给网页。

public ActionResult About()  
{  
    return View(); // 参数可以返回model对象  
} 
(16). PartialViewResult:返回PartialViewResult部分视图结果,主要用于返回部分视图内容。
  在View/Shared目录下创建ViewUserControl.cshtml部分视图。
public ActionResult UserControl()  
{  
     ViewBag.Message = "部分视图";  
     return PartialView("ViewUserControl");  
} 

  页面调用@ViewBag.Message 将输出“部分视图”。

总结:这些返回类型的共同点,那便是对Action有一定的要求:

  • 必须是一个public方法
  • 必须是实例方法
  • 不能被重载
  • 必须返回ActionResult类型
原文地址:https://www.cnblogs.com/wfy680/p/15521280.html