简体   繁体   中英

ASP.NET MVC OutputCache inherited is not working

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

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
{
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM