c# - 按 Url 输出缓存(路由)ASP.NET 4

标签 c# asp.net caching routes

我还没有找到明确的答案,所以有人可以帮助我吗?

如果我们有这样的 URL

 www.website.com/results.aspx?listingtype=2&propertytype=1&location=alaska

然后我们可以设置

 <%@ OutputCache Duration="120" VaryByParam="listingtype;propertytype;location" %>

但是我使用路由,所以我的 url 看起来像这样:

 www.website.com/buy/houses/alaska

或者例如

 www.website.com/rent/condominiums/nevada

我如何在 VaryByParam 中使用 RouteValues,或者我可以从代码隐藏中设置它还是如何设置? 我没有使用 MVC,这是一个 ASP.NET 网站

最佳答案

编辑:(对于非 ASP.NET MVC 应用程序)

这个怎么样:

使 OutputCache 定义如下:


<%@ OutputCache Duration="120" VaryByParam="None" VaryByCustom="listingtype;propertytype;location" %>

在 Global.asax.cs 中添加这些方法:


public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "lisingtype")
    {
        return GetParamFromRouteData("listingtype", context);
    }

    if (custom == "propertytype")
    {
        return GetParamFromRouteData("propertytype", context);
    }

    if (custom == "location")
    {
        return GetParamFromRouteData("location", context);
    }

    return base.GetVaryByCustomString(context, custom);
}

private string GetParamFromRouteData(string routeDataKey, HttpContext context)
{
    object value;

    if (!context.Request.RequestContext.RouteData.Values.TryGetValue(routeDataKey, out value))
    {
        return null;
    }

    return value.ToString();
}

旧内容:

如果您只是将 OutputCache 放在您的操作方法上,并使所有路由部分成为操作方法的一部分,则如下所示:


[OutputCache]
public ActionResult FindProperties(string listingtype, string propertytype, string location)
{
}

框架会自动根据这些项目为您改变缓存(参见:http://aspalliance.com/2035_Announcing_ASPNET_MVC_3_Release_Candidate_2_.4)

关于c# - 按 Url 输出缓存(路由)ASP.NET 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10569849/

相关文章:

c# - 对于循环运行两次

c# - 在映射中保留 null - 不要为类型创建默认值

c# - 如何从 asp.net webform 中的 gridview 优化 "foreach"事件?

asp.net - 报告查看器参数未以编程方式设置

javascript - 检测 Excel 对话框何时出现并发出 javascript 命令

c# - Silverlight 绑定(bind)问题

c# - 从 ASP.net 的提醒开始

android webview缓存图像

java - 将 infinispan 缓存中的数据保存到文件

JavaScript 缓存、Rails 和 Apache 乘客?