Search code examples
asp.netasp.net-web-apidata-caching

Access ASP.Net Data Cache from a Web API method in same ASP.Net project


I need to remove ASP.Net cache from inside a Web API method. This cache by the name of 'ContentNames' was set in the code-behind of an aspx page using following code. Is this possible, and if yes, then how would I access ASP.Net data cache from inside the Web API method?

The Web API and all the aspx pages are part of the same website project in Visual Studio 2013.

System.Web.HttpContext.Current.Cache.Insert("ContentNames", dt, null,
      System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0));

The Web API method from where I need to access and remove 'ContentNames' data cache, looks like below.

    [Authorize]
    [HttpPut]
    public HttpResponseMessage ApproveOrRejectContent( RadEditorContentAdminParas paras)
    {

         var data =  GetUnApprovedContent(paras.ApprovedOrRejected, paras.PageId);

          //NEED to remove a Cache by the name of 'ContentNames' ???

        return Request.CreateResponse<ContentsInfoResult>(HttpStatusCode.OK, data);
    }

Solution

  • Just add reference of System.Web to your Web Api controller and you be able to access Current cache and remove it:

    System.Web.HttpContext.Current.Cache.Remove("ContentNames");