我找到了解决方案:
主要的想法是将tempdata的副本保存在缓存中,并在每次请求时检索它。解决方案是自定义tempdataprovider和简单HTTP模块的组合。另外还有几个助手和静态类。
代码如下:
自定义临时数据提供程序:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
public class CustomTempDataProvider : SessionStateTempDataProvider, IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
}
void application_BeginRequest(object sender, EventArgs e)
{
var httpContext = HttpContext.Current;
var tempData = httpContext.Cache[TempDataKey] ?? new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
httpContext.Items.Add("TempData", tempData);
httpContext.Cache.Remove(TempDataKey);
}
public override void SaveTempData(ControllerContext controllerContext,
IDictionary<string, object> values)
{
HttpContext.Current.Cache.Insert(TempDataKey, values, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
base.SaveTempData(controllerContext, values);
}
public static string TempDataKey
{
get
{
string sessionID = "0";
var httpContext = HttpContext.Current;
if(httpContext.Session != null)
{
sessionID = httpContext.Session.SessionID;
}
else if (httpContext.Request.Cookies["ASP.NET_SessionId"] != null)
{
sessionID = httpContext.Request.Cookies["ASP.NET_SessionId"].Value;
}
return "TempData-For-Session-" + sessionID;
}
}
public void Dispose()
{
}
}
在web.config中注册:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<httpModules>
<add name="CustomTempDataProvider" type="CustomTempDataProvider" />
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="CustomTempDataProvider" />
<add name="CustomTempDataProvider" type="CustomTempDataProvider" />
</modules>
</system.webServer>
</configuration>
自定义控制器工厂:
using System.Web.Routing;
using System.Web.Mvc;
public class CustomControllerFactory : DefaultControllerFactory
{
public override IController CreateController(
RequestContext requestContext, string controllerName)
{
var controller = (Controller)base.CreateController(requestContext, controllerName);
controller.TempDataProvider = new CustomTempDataProvider();
return controller;
}
}
在global.asax中注册:
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory));
}
用于访问tempdata的静态类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public static class CustomTempData
{
public static object Get(string key)
{
var tempData = HttpContext.Current.Items["TempData"] as IDictionary<string, object>;
var item = tempData.FirstOrDefault(x => x.Key == key).Value ?? String.Empty;
return item;
}
}
后缓存替换帮助程序:
using System;
using System.Web;
using System.Web.Mvc;
public static class Html
{
public delegate object MvcResponseSubstitutionCallback(HttpContextBase context);
public static object MvcResponseSubstitute(this HtmlHelper html, MvcResponseSubstitutionCallback callback)
{
html.ViewContext.HttpContext.Response.WriteSubstitution(
context =>
HttpUtility.HtmlEncode(
(callback(new HttpContextWrapper(context)) ?? String.Empty).ToString()
)
);
return null;
}
}
现在这项工作成功:
<h3><%= Html.MvcResponseSubstitute(context => CustomTempData.Get("message")) %></h3>
如果你懂俄语,请阅读
this
希望这有帮助