1
votes

I have an ASP.NET MVC 5 application. This application requests records through WEB API web service. WEB API calls back to a exctetion method that returns results in JSON format. I have not been able to prove this, but I'm concerned that my data may be getting cached.

I only want the caching to be applied to specific actions, not for all actions.

Is there an attribute that I can put on an action to ensure that the data does not get cached? If not, how do I ensure that the browser gets a new set of records each time, instead of a cached set?

I put the below attributes on action to ensure that the data do not get cached.But do not work correctly after insert data. So after reset IIS effected the changes.

[System.Web.Mvc.OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public class UserController : ApiController
{
    [HttpPost]
    [NoCache]
    public List<DataModel> Get()
    {

        var result= ExtMethod.GetData();
        return result;

    }
}
1
Looks like your ExtMethod is static. That might be the reason. - Selçuk
@Selçuk yes that's static method - omid esmaili
Can you add your ExtMethod class code? - Selçuk
@Selçuk Yes,I do - omid esmaili

1 Answers

0
votes

I'm with an ASP.NET MVC5 application as well and added the OutputCache directives to my regular Controllers and my API controller. I still had some caching going on still. It seemed to help in my case to move the cache prevention code into a central location: to the App_Start/FilterConfig.cs's RegisterGlobalFilters method:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        // ...
        filters.Add(new OutputCacheAttribute
        {
            NoStore = true,
            Duration = 0,
            VaryByParam = "*",
            Location = System.Web.UI.OutputCacheLocation.None
        });
    }
}

Once I had that in place I could remove the individual directives from the various Controllers. The downside of this is if you'd want to enable caching from any of the controllers, but my understanding is that you can override the global filter by applying a different OutputCache directive at Controller or View level.