当前位置:   article > 正文

【.NET Core】ActionResult及其常用派生类_iactionresult

iactionresult

ActionResult

ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为一个ContentResult类型。默认的ControllerActionInvoker调用ActionResult.ExecuteResult方法生成应答结果。

ActionResult默认实现 IActionResult

public abstract class ActionResult : Microsoft.AspNetCore.Mvc.IActionResult
  • 1

派生与继承

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

Result的封装

除了通过new对象返回结果外,还可以使用封装后的方法;

在这里插入图片描述
示例:

        public IActionResult Result1()//实例化对象
        {
            JsonResult result = new JsonResult(new { name = "kxy1" });
            return result;
        }
        public IActionResult Result2()//封装方法
        {
            return Json(new { name = "kxy2" });
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

扩展ActionResult

下例将实现一个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());
                }
            }
        }
    } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

.NET MVC下ActionResult(12种)的简单应用

注: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();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169

来源

ActionResult 详解
ActionResult 类
MVC中几种常用ActionResult

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/707145
推荐阅读
相关标签
  

闽ICP备14008679号