将 DefaultCONtrollerFactory 更改为自定义 ControllerFactory 类。默认 Controller.TempDataProvider 使用 SessionStateTempDataProvider。你可以改变它。
1.设置web.config/system.web/sessionState:mode="Off"。
2.创建 DictionaryTempDataProvider 类。
public class DictionaryTempDataProvider : ITempDataProvider
{
public IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
{
return new Dictionary<string, object>();
}
public void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
{
}
}
3.创建 DictionaryTempDataControllerFactory
public class DictionaryTempDataControllerFactory : DefaultControllerFactory
{
public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
var controller = base.CreateController(requestContext, controllerName) as Controller;
if (controller!=null)
controller.TempDataProvider = new DictionaryTempDataProvider();
return controller;
}
}
4.在 global.asax.cs Apprication_Start 事件中设置 DictionaryTempDataControllerFactory。
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(
new DictionaryTempDataControllerFactory()
);
}