ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中...

我们在ASP.NET Core MVC项目中有如下HomeController:

using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreActionFilter.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 显示一个网页供测试
        /// </summary>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 返回一个Json对象到客户端浏览器
        /// </summary>
        /// <returns>Json对象</returns>
        public IActionResult GetJson()
        {
            return Json(new { Message = "This is a GetJson message!" });
        }
    }
}

其代码非常简单,HomeController只有两个Action:

  • Index这个Action,输出一个简单的网页供测试
  • GetJson这个Action,输出一个Json对象到客户端浏览器

 

现在我们在浏览器上输入Url地址"Home/GetJson"来访问HomeController的GetJson这个Action,结果如下:

通过IE浏览器的开发者工具,我们看到,浏览器成功接收到了HomeController的GetJson这个Action返回的Json对象,其message属性值为我们在GetJson这个Action中返回的"This is a GetJson message!"。

 

然后我们定义一个IActionFilter拦截器叫MyActionFilterAttribute,利用IActionFilter的OnActionExecuted方法,我们可以在HomeController的GetJson这个Action方法执行后,替换GetJson方法返回的Json对象:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;

namespace AspNetCoreActionFilter.Filters
{

    /// <summary>
    /// 定义一个IActionFilter拦截器叫MyActionFilterAttribute
    /// </summary>
    public class MyActionFilterAttribute : Attribute, IActionFilter
    {
        /// <summary>
        /// IActionFilter.OnActionExecuted在Controller的Action方法执行完后执行
        /// </summary>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            //等Controller的Action方法执行完后,通过更改ActionExecutedContext类的Result属性,来替换Action方法返回的Json对象
            context.Result = new JsonResult(new { Message = "This is a MyActionFilter message!" });
        }

        /// <summary>
        /// IActionFilter.OnActionExecuting在Controller的Action方法执行前,但是Action方法的参数模型绑定完成后执行
        /// </summary>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            //Do something...
            //context.Result = new EmptyResult();//在IActionFilter.OnActionExecuting方法中,context的Result属性只要被赋值了不为null,就不会执行Controller的Action了,也不会执行该IActionFilter拦截器的OnActionExecuted方法,同时在该IActionFilter拦截器之后注册的其它Filter拦截器也都不会被执行了
        }
    }
}

 

但问题是当ASP.NET Core MVC执行IActionFilter的OnActionExecuted方法时,HomeController的GetJson这个Action方法返回的Json对象,是否已经被输出到Http Response中发送给客户端浏览器了呢?

 

我们将上面定义的IActionFilter拦截器MyActionFilterAttribute注册到HomeController的GetJson方法上:

using AspNetCoreActionFilter.Filters;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreActionFilter.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 显示一个网页供测试
        /// </summary>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 返回一个Json对象到客户端浏览器
        /// </summary>
        /// <returns>Json对象</returns>
        [MyActionFilter]
        public IActionResult GetJson()
        {
            return Json(new { Message = "This is a GetJson message!" });
        }
    }
}

然后再在浏览器中输入Url地址"Home/GetJson"来访问HomeController的GetJson这个Action,这次结果如下:

我们可以看到这次浏览器收到的Json对象是MyActionFilterAttribute拦截器中OnActionExecuted方法替换的Json对象,其message属性值为"This is a MyActionFilter message!",而HomeController的GetJson这个Action返回的Json对象根本就没有被浏览器收到,说明HomeController的GetJson这个Action返回的Json对象完全没有被输出到Http Response中发送给客户端浏览器。所以这很好地说明了ASP.NET Core MVC中IActionFilter拦截器的OnActionExecuted方法,会在Controller的Action返回的对象被输出到Http Response之前执行。

 

我们还可以在ASP.NET Core MVC中的.cshtml视图文件中写几个C#代码打上断点,在IActionFilter拦截器的OnActionExecuted方法中也打上断点,在Visual Studio调试中我们会发现,IActionFilter拦截器的OnActionExecuted方法的断点先被执行,然后才执行.cshtml视图文件中的断点,这也很清晰地证明了上面黑色粗体字的观点。当然如果你在Controller的Action方法中,直接使用Response.Body的Stream流输出数据到客户端浏览器,IActionFilter拦截器的OnActionExecuted方法执行时数据肯定已经发送给客户端浏览器了。

 

转载于:https://www.cnblogs.com/OpenCoder/p/9835989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值