ASP.NET MVC OutputCache inherited is not working

380 views Asked by At

Hello I am trying using the OutputCache inheriting a base controller. But it is not working, my codes:

BaseController

 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);
    }
}

Controller

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

View

@System.DateTime.Now

But just not working. It only works if I did not inherit, or I remove the code below the base controller

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);
    //}

}

I think I have to do some implementation in protected override void Initialize.

Detail: I need this method, as there is variables that use other controllers

1

There are 1 answers

2
Nick On

I have dealt with this before and found that some pre existing Attributes cannot be inherited because they were designed that way in their declaration.

The only way that I found is to create a new attribute that extends the one you want to use but marked as Inherited, liked this:

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