VaryByParam="*" 是否有效?还阅读 RouteData.Values?

发布于 2024-10-09 02:07:33 字数 1025 浏览 0 评论 0 原文

在我的 asp.net mvc 项目中,我在控制器上启用输出缓存,如下所示,

[OutputCache(Duration = 100, VaryByParam = "*", VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
    public ActionResult Index(string seller)
    {
        // I do something
    }
}

它效果很好,直到创建我自己的 Route 类,如下所示

public class MyRoute : Route
{
    // there is a constructor here..

    // I override this method.. 
    // just to add one data called 'seller' to RouteData
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var data = base.GetRouteData(httpContext);
        if (data == null) return null;

        var seller = DoSomeMagicHere();

        // add seller
        data.Values.Add("seller", seller);

        return data;
    }

}

,然后,操作方法将采用 seller 作为参数。我通过始终提供不同的 seller 参数来测试它,但它从缓存中获取输出而不是调用该方法。

在 asp.net mvc 中,设置 VaryByParam="*" 是否也会因 RouteData.Values 而异?

我正在使用 ASP.Net 4 MVC 3 RC 2

in my asp.net mvc project, I enable output caching on a controller as below

[OutputCache(Duration = 100, VaryByParam = "*", VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
    public ActionResult Index(string seller)
    {
        // I do something
    }
}

it works great, until create my own Route class as below

public class MyRoute : Route
{
    // there is a constructor here..

    // I override this method.. 
    // just to add one data called 'seller' to RouteData
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var data = base.GetRouteData(httpContext);
        if (data == null) return null;

        var seller = DoSomeMagicHere();

        // add seller
        data.Values.Add("seller", seller);

        return data;
    }

}

and then, the action method will take seller as parameter. I tested it by always providing different seller parameter, but it take the output from cache instead of calling the method.

does setting VaryByParam="*" also vary by RouteData.Values, in asp.net mvc?

I'm using ASP.Net 4 MVC 3 RC 2

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

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

发布评论

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

评论(2

翻身的咸鱼 2024-10-16 02:07:33

输出缓存机制因 URL、QueryString 和 Form 的不同而异。 RouteData.Values 此处未表示。原因是输出缓存模块在路由之前运行,因此当第二个请求进入并且输出缓存模块正在寻找匹配的缓存条目时,它甚至没有要检查的 RouteData 对象

通常这不是问题,因为 RouteData.Values 直接来自 URL,而 URL 已经被考虑在内。如果您想根据某个自定义值进行更改,请使用 VaryByCustomGetVaryByCustomString 来完成此操作。

The output caching mechanism varies by URL, QueryString, and Form. RouteData.Values is not represented here. The reason for this is that the output caching module runs before Routing, so when the second request comes in and the output caching module is looking for a matching cache entry, it doesn't even have a RouteData object to inspect.

Normally this isn't a problem, as RouteData.Values comes straight from the URL, which is already accounted for. If you want to vary by some custom value, use VaryByCustom and GetVaryByCustomString to accomplish this.

生活了然无味 2024-10-16 02:07:33

如果您删除 VaryByParam = "*" 它应该在缓存时使用您的操作方法参数值。

ASP.NET MVC 3 的输出缓存系统不再需要您
声明 [OutputCache] 时指定 VaryByParam 属性
控制器操作方法上的属性。 MVC3 现在自动
当您的操作方法上有显式参数时,会改变输出缓存条目 - 允许您干净地启用输出...

来源:http://weblogs.asp.net/scottgu/archive/2010/12/ 10/announcing-asp-net-mvc-3-release-candidate-2.aspx

[OutputCache(Duration = 100, VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
    public ActionResult Index(string seller)
    {
        // I do something
    }
}

If you remove VaryByParam = "*" it should use your action method parameter values when caching.

ASP.NET MVC 3’s output caching system no longer requires you to
specify a VaryByParam property when declaring an [OutputCache]
attribute on a Controller action method. MVC3 now automatically
varies the output cached entries when you have explicit parameters on your action method – allowing you to cleanly enable output...

Source: http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx

[OutputCache(Duration = 100, VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
    public ActionResult Index(string seller)
    {
        // I do something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文