【问题标题】:ASP.NET MVC OutputCache inherited is not working继承的 ASP.NET MVC OutputCache 不起作用
【发布时间】:2015-06-19 12:52:07
【问题描述】:

您好,我正在尝试使用继承基本控制器的 OutputCache。但它不起作用,我的代码:

基础控制器

 public abstract partial class BasicControllerController : Controller
{

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        if (requestContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }
        base.Initialize(requestContext);
    }
}

控制器

 [OutputCache(Duration=60)]
public class SamplesController : BasicControllerController
{
    public ActionResult AgendamentoEmail()
    {
        return View();
    }
}

查看

@System.DateTime.Now

但就是不工作。 仅当我没有继承或删除基本控制器下方的代码时才有效

public abstract partial class BasicControllerController : Controller
{
    //protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    //{
    //    if (requestContext == null)
    //    {
    //        throw new ArgumentNullException("filterContext");
    //    }
    //    base.Initialize(requestContext);
    //}

}

我想我必须在受保护的覆盖无效初始化中做一些实现。

详情:我需要这个方法,因为有使用其他控制器的变量

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 outputcache


    【解决方案1】:

    我之前处理过这个问题,发现一些预先存在的属性不能被继承,因为它们在声明中是这样设计的。

    我发现的唯一方法是创建一个新属性,扩展您要使用但标记为继承的属性,喜欢这个:

    [AttributeUsage (Inherited = True)]
    public class InheritedOutputCacheAttribute : OutputCacheAttribute
    {
    ...
    }
    

    【讨论】:

    • Helo.. 坦克... 我试过了,但没用.. 你有更详细的例子吗?[AttributeUsage(AttributeTargets.All,Inherited=false)] public class DonutCacheAttribute : OutputCacheAttribute { public DonutCacheAttribute() { // get cache duration from web.config Duration = Settings.DonutCachingDuration; } }
    • Inherited 应该是 true,而不是 false...你试过了吗?
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 2014-09-04
    • 2019-05-05
    • 2018-02-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多