Disable IIS cache

Asked

Viewed 549 times

4

I’m having problems with IIS cache (I think the problem is he), whenever I make any changes in the database, changes do not happen on the website, keeps the old data.

Changes only appear when I turn off and on IIS.

Attempts

I added this command to web.config

<caching enabled="false" />

In the controller, I also added this Annotation

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

And finally, in the global.asax added this method

protected void Application_BeginRequest()
{
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();
}

None of these attempts worked, maybe the problem isn’t cache, I don’t know...

Controller

public class CursosController : Controller
{
    private SiteContext db = new SiteContext();

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult Index(Cursos curso)
    {
        return View(curso);
    }        
}
  • Leonardo, I believe that disabling the Cache is not the best way, I imagine some more problem of structure of your project.

  • If possible, share your code controller.

  • @Pablovargas I added the code of controller.

  • Try to use [OutputCache(Location = OutputCacheLocation.None)]

  • 1

    Why not seek the courses within the method Index?

  • @Marllonnasser Good question, I didn’t even realize that he passes the Courses to Index

  • @Pablovargas didn’t work out using this code you sent.

  • 1

    Change your Index not to receive the Courses but to search in the bank before the return, being able to stay like this public ActionResult Index()&#xA; {&#xA; return View(db.Cursos.ToList());&#xA; }

  • @Marllonnasser I’m passing the Curso via MvcRouteHandler.GetHttpHandler. I’ll change and try to search through the Index.

  • @Pablovargas in fact in Index I will need to receive a parameter via GET, because I will show a single course. site.com/curso/administracao

  • 1

    get the course data in real time... that is, before going to the view, fetch the database data for the course you want and then throw the filled object from the bank to the view.

  • Man, this happened to me a few times already. I always thought it was something from the other side because I never saw anyone complain about it. I am waiting for an answer and also an explanation for this to happen.

  • 1

    Then do the following in your index public ActionResult Index(int id) { return View(db.Cursos.FirstOrDefault(a=>a.Id == id)); }

  • @Pablovargas It worked, the changes are being displayed correctly. I changed the logic and query is being made only in the controller.

  • @jbueno I was able to solve my problem by only changing the place where the database query is being made.

  • @Leonardo I don’t understand. It’s an example?

  • I think the answer is up to you to publish it yourself. Explain well what you did to fix your problem

  • @Pablovargas Okay. As soon as I have a little time I’ll draft an answer!

Show 13 more comments

1 answer

1


I changed my Controller to carry out the consultation of the Course in the database, and not receive from elsewhere, thus staying:

public ActionResult Index(string parametro)
{
    var curso = db.Cursos.FirstOrDefault(x => x.Slug == parametro);
    if (curso == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.NotFound);
    }
    return View(curso);
}

Note that I am now receiving as a parameter a string and not a Curso, different from how I was in the question. Also no longer need to put the following annotation

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.