【发布时间】:2017-03-01 10:48:08
【问题描述】:
我正在使用下面的代码来呈现页面并将输出存储在缓存中。这样在下一个请求中页面来自缓存(以减少处理时间)
HelperResult renderPageResult = null;
try
{
if (objCache.Get(new CacheService().GetKey(cacheKey, _userContext)) == null)
{
renderPageResult = RenderPage(path, data);
objCache.Add(new CacheService().GetKey(cacheKey, _userContext), renderPageResult);
}
else
{
renderPageResult = objCache.Get<HelperResult>(new CacheService().GetKey(cacheKey, _userContext));
}
return renderPageResult;
}
catch(Exception ex)
将对象转换回 HelperResult 时,我没有得到 HTML 字符串,并且在 UI 上得到以下异常
值不在预期范围内。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.ArgumentException:值不在预期范围内。
【问题讨论】:
-
哪行代码抛出异常?
-
我得到的不是html字符串,而是对象
-
如果我正确理解您的问题
objCache.Get方法返回对象而不是类HelperResult的对象。当您尝试在 UI 中呈现它时,您会看到异常。如果我错了,请纠正我?您能否提供更多关于您要实现的目标以及您在哪里使用HelperResult对象的上下文? -
如果布局中的 RenderPage 很少。我不想一次又一次地渲染它们。我希望它们只呈现一次并将输出存储在缓存中下次有请求时不会调用 Renderpage 并将输出从缓存发送到布局页面
-
那么你如何尝试渲染从缓存中检索到的 HelperResult?
标签: c# asp.net-mvc-4 html-helper