我正在使用CachedPartial html 助手缓存该部分 View 。
@Html.CachedPartial("PartialView", MyModel, 3600, true);
在我看来,我有以下情况:
@Html.CachedPartial("PartialView", MyModel, 3600, true);
@Html.CachedPartial("AnotherPartialView", MyModel1, 3600, true);
@Html.CachedPartial("PartialView", MyModel3, 3600, true); // I want to reuse partial view
由于CachedPartial
,第一个和第三个 View 似乎是相同的...
如何通过模型参数制作缓存部分?
我尝试使用
@Html.CachedPartial("PartialView", MyModel, 3600, true, false, new ViewDataDictionary(MyModel3));
但同样的事情。
<小时/>编辑:我使用了与 DZL 不同的方法,并且它有效
public static IHtmlString CachedPartial( this HtmlHelper helper, string partialViewName, object model, string cacheKey = null )
{
if ( string.IsNullOrWhiteSpace( cacheKey ) ) {
return helper.CachedPartial( partialViewName, model, AppSettings.PartialCachingSeconds, true );
}
Func<object, ViewDataDictionary, string> fc = ( o, v ) => cacheKey;
return helper.CachedPartial( partialViewName, model, AppSettings.PartialCachingSeconds, true, contextualKeyBuilder: fc );
}
然后
@Html.CachedPartial("PartialView", MyModel, "a_key");
@Html.CachedPartial("AnotherPartialView", MyModel1);
@Html.CachedPartial("PartialView", MyModel3, "another_key"); // I want to reuse partial view
最佳答案
如果您愿意,您需要创建自己的 CachedPartial
实现,如下所示:
using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Umbraco.Web;
using System.Web;
using System.Runtime.Caching;
public static class CachedPartialExtensions
{
public static IHtmlString MyCachedPartial(
this HtmlHelper htmlHelper,
string partialViewName,
object model,
int cachedSeconds,
bool cacheByPage = false,
string cacheKey = null,
ViewDataDictionary viewData = null
)
{
var newCacheKey = "fpc-"; //prefix to know which keys to clear on page publish (in Bootstraper.cs file)
newCacheKey += partialViewName;
if (cacheByPage)
{
newCacheKey += "page-" + UmbracoContext.Current.PageId;
}
if (!string.IsNullOrEmpty(cacheKey))
{
newCacheKey += "key-" + cacheKey;
}
var result = MemoryCache.Default.Get(newCacheKey) as MvcHtmlString;
if(result == null)
{
result = htmlHelper.Partial(partialViewName, model, viewData);
MemoryCache.Default.Add(new CacheItem(newCacheKey, result), new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(cachedSeconds)
});
}
return result;
}
}
然后您将能够提供自己的缓存 key :
@Html.MyCachedPartial("PartialView", Model, 60, cacheKey: "model1key", cacheByPage: true)
@Html.MyCachedPartial("PartialView", Model2, 60, cacheKey: "model2key", cacheByPage: true)
编辑:
从版本 7 开始,CachedPartial
有一个重载,允许传入 key
public static IHtmlString CachedPartial(
this HtmlHelper htmlHelper,
string partialViewName,
object model,
int cachedSeconds,
bool cacheByPage = false,
bool cacheByMember = false,
ViewDataDictionary viewData = null,
Func<object, ViewDataDictionary, string> contextualKeyBuilder = null);
其用例是:
@Html.CachedPartial(
"PartialView",
MyModel3,
3600,
cacheByPage: true,
contextualKeyBuilder: (model, viewData) =>
{
return (model as MyViewModel).Id.ToString();
});
关于asp.net-mvc - 使用 Umbraco CachedPartial 为不同模型缓存部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260172/