首页 > 解决方案 > how to set Response.Cache in abstract class

问题描述

I have this abstract class

public abstract class TestControllerBase {

}

and a class

public class TestController : TestControllerBase {
    public ActionResult Index()
    {
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetExpires(DateTime.Now.AddHours(1));

    }    

}

how can i set Response.Cache in the abstract class without excplicit calling something in this abstract class. I have like 200 inheritence on the abstract class. So i want to set the cache in one place not in every controller

Regards

标签: c#

解决方案


您可以使用以下代码:

public abstract class TestControllerBase
{

    public virtual ActionResult Index()
    {
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetExpires(DateTime.Now.AddHours(1));

    } 
}

public class TestController : TestControllerBase
{
}

public class TestController2 : TestControllerBase
{

    public override ActionResult Index()
    {
        // Do my own stuff

        base.Index();
    } 
}

推荐阅读