开发者

How do I cache an MVC3 ActionResult that returns Json?

I have a controller ActionResult that returns JSON that I would like to cache. After doing some research, I have found that the OutputCache Attribute can do the trick, but haven't been able to successfully use it. Here's the method call that I'm using it on:

    [OutputCache(Duration = 86400, VaryByParam = "none")]
    public ActionResult GetCategories()
    {
        var request = (HttpWebRequest)WebRequest.Create(EXTERNAL API CALL);
        request.Accept = "application/json; charset=utf-8";
        var response = (HttpWebResponse)request.GetResponse();
        string result;
        using (var sr = new StreamReader(response.GetResponseStream()))
        {
          开发者_开发技巧  result = sr.ReadToEnd();
        }
        return Json(result);
    }

This method makes an API call to an outside source, reads the result and then returns the result as a json to an AJAX call. With the OutputCacheAttribute in place, the ajax call returns a 500 (Internal Server Error). Without this attribute, it works just fine.

Am I doing anything wrong, or is there a better suggestion that I should use for this situation?

Thanks!


In case someone has a similar issue, I figured out the problem. I had CacheProfile set to a value that didn't exist in the web.config. I know I didn't have that attribute set in my question, so... sorry if there was any confusion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜