locked
MVC5 Outputcache generate with webrequest calls RRS feed

  • Question

  • User-435676276 posted

    In my  MVC application Outputcache is not working as I am expecting & description. I want to build outputcache from a webrequest call, the outputcache location is on the server.
    Browsers manage to pick up correctly the cached page after a  browser request but do not pick up the cache after a webrequest.  
    Any idea of how to see what is the outputcache keys, because that is the issue webrequest or adds a different key or something in the outputcache algorithm differentiates explicitly?  

    Tuesday, May 19, 2020 2:31 PM

All replies

  • User475983607 posted

    In my  MVC application Outputcache is not working as I am expecting & description. I want to build outputcache from a webrequest call, the outputcache location is on the server.
    Browsers manage to pick up correctly the cached page after a  browser request but do not pick up the cache after a webrequest.  
    Any idea of how to see what is the outputcache keys, because that is the issue webrequest or adds a different key or something in the outputcache algorithm differentiates explicitly?  

    Make sure you are populating the cache control headers in the web request.  See the specs.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

    Tuesday, May 19, 2020 3:09 PM
  • User-435676276 posted

    The request headers from browser it's replicated on the webrequest. Everything is there and also the webrequest caching is working, just browsers keep a separate cache from webrequests. 

    Tuesday, May 19, 2020 3:53 PM
  • User1686398519 posted

    Hi,  Lajos Simon

    Do you mean that after a browser request, and then make a webrequest request to get cached? If so, you need to add "VaryByParam =" none "".

    You can refer to this link for other usages of "VaryByParam".

    I made a simple example please refer to it.

    Controller

    public ActionResult WebRequestExample(){
    WebRequest request = WebRequest.Create("https://localhost:44323/Home/Index");
    request.Credentials = CredentialCache.DefaultCredentials;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
    response.Close();
    return Content(responseFromServer);
    }
    [OutputCache(Duration = 10, VaryByParam = " none", Location = OutputCacheLocation.Server)]
    public ActionResult Index()
    {
    var timeStr = DateTime.Now.ToString();
    ViewBag.timeStr = timeStr;
    return View();
    }

    Index

    @ViewBag.timeStr

    Here is the result.

     
    Best Regards,

    YihuiSun

    Thursday, May 21, 2020 6:57 AM
  • User-435676276 posted

    That's already in place, was first to think about,  It's failing because of something more tricky. 

    Thursday, June 4, 2020 12:16 AM