赞
踩
ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker调用ActionResult.ExecuteResult方法生成应答结果。
ActionResult默认实现 IActionResult
public abstract class ActionResult : Microsoft.AspNetCore.Mvc.IActionResult
ActionResult继承与Object,mvc的返回result基本都继承于ActionResult
派生:
/* 返回文件内容。FilePath通过路径传送文件到客户端,FileContent通过二进制数据的方式,而FileStream是通过Stream的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。 */ Microsoft.AspNetCore.Mvc.ContentResult ```csharp /* 返回文件内容。FilePath通过路径传送文件到客户端,FileContent通过二进制数据的方式,而FileStream是通过Stream的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。 */ Microsoft.AspNetCore.Mvc.ChallengeResult /* 返回简单的纯文本内容,可通过ContentType属性指定应答文档类型,通过ContentEncoding属性指定应答文档的字符编码。可通过Controller类中的Content方法便捷地返回ContentResult对象。如果控制器方法返回非ActionResult对象,MVC将简单地以返回对象的ToString()内容为基础产生一个ContentResult对象。 */ Microsoft.AspNetCore.Mvc.ContentResult /* 返回一个空的结果。如果控制器方法返回一个null,MVC将其转换成EmptyResult对象。 */ Microsoft.AspNetCore.Mvc.EmptyResult /*FilePathResult、FileContentResult、FileStreamResult这三个类继承于FileResult,表示一个文件内容,三者区别在于,FilePath 通过路径传送文件到客户端,FileContent 通过二进制数据的方式,而FileStream 是通过Stream(流)的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。 FilePathResult: 直接将一个文件发送给客户端 FileContentResult: 返回byte字节给客户端(比如图片) FileStreamResult: 返回流*/ Microsoft.AspNetCore.Mvc.FileResult Microsoft.AspNetCore.Mvc.ForbidResult /* 返回Json格式数据。 MVC将Response.ContentType设置为application/json,并通过JavaScriptSerializer类将指定对象序列化为Json表示方式。需要注意,默认情况下,MVC不允许GET请求返回JSON结果,要解除此限制,在生成JsonResult对象时,将其JsonRequestBehavior属性设置为JsonRequestBehavior.AllowGet。此结果对应的Controller方法为Json。 */ Microsoft.AspNetCore.Mvc.JsonResult Microsoft.AspNetCore.Mvc.LocalRedirectResult Microsoft.AspNetCore.Mvc.ObjectResult /*接收分部视图引擎的响应*/ Microsoft.AspNetCore.Mvc.PartialViewResult Microsoft.AspNetCore.Mvc.RazorPages.PageResult /*表示一个连接跳转,相当于ASP.NET中的Response.Redirect方法。对应的Controller方法为Redirect。*/ Microsoft.AspNetCore.Mvc.RedirectResult Microsoft.AspNetCore.Mvc.RedirectToActionResult Microsoft.AspNetCore.Mvc.RedirectToPageResult /*同样表示一个跳转,MVC会根据我们指定的路由名称或路由信息(RouteValueDictionary)来生成Url地址,然后调用Response.Redirect跳转。对应的Controller方法为RedirectToAction和RedirectToRoute.*/ Microsoft.AspNetCore.Mvc.RedirectToRouteResult Microsoft.AspNetCore.Mvc.SignInResult Microsoft.AspNetCore.Mvc.SignOutResult Microsoft.AspNetCore.Mvc.StatusCodeResult Microsoft.AspNetCore.Mvc.ViewComponentResult /*接收视图引擎的响应*/ Microsoft.AspNetCore.Mvc.ViewResult
除了通过new对象返回结果外,还可以使用封装后的方法;
示例:
public IActionResult Result1()//实例化对象
{
JsonResult result = new JsonResult(new { name = "kxy1" });
return result;
}
public IActionResult Result2()//封装方法
{
return Json(new { name = "kxy2" });
}
下例将实现一个XmlResult类型,用于返回XML应答内容:
public class XmlResult : ActionResult { public XmlResult(Object data) { this.Data = data; } public Object Data { get; set; } public override void ExecuteResult(ControllerContext context) { if (Data == null) { new EmptyResult().ExecuteResult(context); return; } context.HttpContext.Response.ContentType = "application/xml"; using (MemoryStream ms = new MemoryStream()) { XmlSerializer xs = new XmlSerializer(Data.GetType()); xs.Serialize(ms, Data); ms.Position = 0; using (StreamReader sr = new StreamReader(ms)) { context.HttpContext.Response.Output.Write(sr.ReadToEnd()); } } } }
注:AspNetCore.Mvc不一定兼容
using StudyMVC4.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Mvc; namespace StudyMVC4.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } /// <summary> /// ContentResult用法(返回文本) /// http://localhost:30735/home/ContentResultDemo /// </summary> /// <returns>返回文本</returns> public ActionResult ContentResultDemo(){ string str = "ContentResultDemo!"; return Content(str); } /// <summary> /// EmptyResult的用法(返回空对象) /// http://localhost:30735/home/EmptyResultDemo /// </summary> /// <returns>返回一个空对象</returns> public ActionResult EmptyResultDemo (){ return new EmptyResult(); } /// <summary> /// FileContentResult的用法(返回图片) /// http://localhost:30735/home/FileContentResultDemo /// </summary> /// <returns>显示一个文件内容</returns> public ActionResult FileContentResultDemo() { FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read); byte[] buffer = new byte[Convert.ToInt32(fs.Length)]; fs.Read(buffer, 0, Convert.ToInt32(fs.Length)); string contentType = "image/jpeg"; return File(buffer, contentType); } /// <summary> /// FilePathResult的用法(返回图片) /// http://localhost:30735/home/FilePathResultDemo/002 /// </summary> /// <param name="id">图片id</param> /// <returns>直接将返回一个文件对象</returns> public FilePathResult FilePathResultDemo(string id) { string path = Server.MapPath(@"/Images/"+id +".jpg"); //定义内容类型(图片) string contentType = "image/jpeg"; //FilePathResult直接返回file对象 return File(path, contentType); } /// <summary> /// FileStreamResult的用法(返回图片) /// http://localhost:30735/home/FileStreamResultDemo /// </summary> /// <returns>返回文件流(图片)</returns> public ActionResult FileStreamResultDemo() { FileStream fs = new FileStream(Server.MapPath(@"/Images/001.jpg"), FileMode.Open, FileAccess.Read); string contentType = "image/jpeg"; return File(fs, contentType); } /// <summary> /// HttpUnauthorizedResult 的用法(抛出401错误) /// http://localhost:30735/home/HttpUnauthorizedResult /// </summary> /// <returns></returns> public ActionResult HttpUnauthorizedResultDemo() { return new HttpUnauthorizedResult(); } /// <summary> /// HttpStatusCodeResult的方法(返回错误状态信息) /// http://localhost:30735/home/HttpStatusCodeResult /// </summary> /// <returns></returns> public ActionResult HttpStatusCodeResultDemo() { return new HttpStatusCodeResult(500, "System Error"); } /// <summary> /// HttpNotFoundResult的使用方法 /// http://localhost:30735/home/HttpNotFoundResultDemo /// </summary> /// <returns></returns> public ActionResult HttpNotFoundResultDemo() { return new HttpNotFoundResult("not found action"); } /// <summary> /// JavaScriptResult 的用法(返回脚本文件) /// http://localhost:30735/home/JavaScriptResultDemo /// </summary> /// <returns>返回脚本内容</returns> public ActionResult JavaScriptResultDemo() { return JavaScript(@"<script>alert('Test JavaScriptResultDemo!')</script>"); } /// <summary> /// JsonResult的用法(返回一个json对象) /// http://localhost:30735/home/JsonResultDemo /// </summary> /// <returns>返回一个json对象</returns> public ActionResult JsonResultDemo() { var tempObj = new { Controller = "HomeController", Action = "JsonResultDemo" }; return Json(tempObj); } /// <summary> /// RedirectResult的用法(跳转url地址) /// http://localhost:30735/home/RedirectResultDemo /// </summary> /// <returns></returns> public ActionResult RedirectResultDemo() { return Redirect(@"http://wwww.baidu.com"); } /// <summary> /// RedirectToRouteResult的用法(跳转的action名称) /// http://localhost:30735/home/RedirectToRouteResultDemo /// </summary> /// <returns></returns> public ActionResult RedirectToRouteResultDemo() { return RedirectToAction(@"FileStreamResultDemo"); } /// <summary> /// PartialViewResult的用法(返回部分视图) /// http://localhost:30735/home/PartialViewResultDemo /// </summary> /// <returns></returns> public PartialViewResult PartialViewResultDemo() { return PartialView(); } /// <summary> /// ViewResult的用法(返回视图) /// http://localhost:30735/home/ViewResultDemo /// </summary> /// <returns></returns> public ActionResult ViewResultDemo() { //如果没有传入View名称, 默认寻找与Action名称相同的View页面. return View(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。