C# mvc统一通道使用过滤器

问题描述

使用C#过滤器有一个最大的问题就是在过滤器转向后程序仍然会执行方法体

问题解决思路

使用统一通道执行方法 不直接进入控制器 通过反射调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace WebApplication2.Controllers
{
    public class DefaultController : Controller
    {
        bool filter = true;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
             filter = false;
             Type t = typeof(DefaultController);
             MethodInfo mf = t.GetMethod("Index");
             mf.Invoke(this,null);
          
        }
        // GET: Default
        public ActionResult Index()
        {
            //Response.Write("我就是我");
            //Response.Flush();
            return View();

        }
    }
}

  对于反射调用方法可使用缓存进行优化

原文地址:https://www.cnblogs.com/ProDoctor/p/6056206.html