ASP.NET MVC 输出缓存 JSONP

发布于 2024-08-23 12:23:06 字数 1022 浏览 5 评论 0原文

我缓存了 ASP.NET MVC 网站上所有可能的内容,并且它运行得非常完美。现在我创建了一个 API,其中的调用转到控制器操作。 (http://mysite.com/topics/latest.json)

该 API 能够以不同格式返回结果(json、xml、rss)。 返回的数据在Action中加载:

[ResponseFilter]
public class HotTopicsController : Controller
{

   [OutputCache(Duration = 60, VaryByParam = "none")]
   public ActionResult Latest()
   {
      ViewData.Model = MyService.GetRepository().ApiViewData().Topics().Latest();

      return View();
   }
}

ResponseFilter负责以正确的格式(json、rss、xml)返回数据。

由于不可能从另一个域发出 JSON 请求(我想让 API 可供其他域使用),所以我必须使用 JSONP。 JSONP 需要一个回调集。

需要在响应中设置回调名称,我无法使用 OutputCache 进行默认缓存。

我知道有关甜甜圈缓存的文章(Phil Haacked:http://haacked.com/archive/2008/11/05/donut-caching-in-asp.net-mvc.aspx 等)。但他们都在视图中处理这个主题。由于我只是设置了 ViewData.Model 并且没有视图,所以我无法以这种方式解决问题。

您对解决这个问题有什么建议?

I cache everything that is possible on an ASP.NET MVC website and it works perfect. Now I have created an API where the calls go to Controller Actions. (http://mysite.com/topics/latest.json)

The API is able to return results in different formats (json, xml, rss).
The data for returning is loaded in the Action:

[ResponseFilter]
public class HotTopicsController : Controller
{

   [OutputCache(Duration = 60, VaryByParam = "none")]
   public ActionResult Latest()
   {
      ViewData.Model = MyService.GetRepository().ApiViewData().Topics().Latest();

      return View();
   }
}

The ResponseFilter is responsible for returning the data in the right format (json, rss, xml).

As it is not possible to make JSON requests from another domain (i want to make the API available for others) I have to use JSONP. JSONP needs a callback set.

The need for setting the name of the callback in the response I am not able to do the default caching with OutputCache.

I know the articles about donut caching (Phil Haacked: http://haacked.com/archive/2008/11/05/donut-caching-in-asp.net-mvc.aspx and others). But they all handle this topic within Views. As I just set ViewData.Model and do not have a view, I am not able to solve the problem in this way.

What are your suggestions to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2024-08-30 12:23:06

您始终可以使用预定义的回调名称。 jQuery.ajax 等客户端允许您指定回调参数的名称。

You could always use a predefined callback name. Clients such as jQuery.ajax allow you to specify the name of the callback parameter.

漫雪独思 2024-08-30 12:23:06

一种答案是使用 GetScript 而不是 GetJSON。
我在这里写了关于它的博客: http://mfriis.blogspot .com/2012/03/caching-jsonp-requests-in-mvc3.html

然而,我的解决方案是基于 MVC3 的。

One answer is to use GetScript instead of GetJSON.
I blogged about it here: http://mfriis.blogspot.com/2012/03/caching-jsonp-requests-in-mvc3.html

my solution is based on MVC3 however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文