我想在HTML助手上使用输出缓存。但是,即使使用属性集,当调用Helper方法时,始终输入此代码块。由于outputcache属性在这种情况下不起作用,因此在Html Helpers中缓存“昂贵”查询的推荐方式是什么?在MVC HTML Helper中缓存“昂贵”查询的推荐方式是什么?
[OutputCache(Duration = 60)]
public static MvcHtmlString CountryDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object selectedValue)
{
var doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/countries.xml"));
var items = new Dictionary<string, string>();
foreach (XmlNode node in doc.SelectNodes("//country"))
{
items.Add(node.InnerText, node.InnerText);
}
return html.DropDownListFor(expression, new SelectList(items, "key", "value", selectedValue));
}