我正在尝试使用TempData将数据从一个请求发送到另一个请求。实现ITempDataProvider vs使用Cookies
但是,由于TempData使用的是服务器会话,并且此应用程序将用于web养殖,所以我将不得不使用cookie或数据库持久性,因为会话不会从一台服务器转移到另一台服务器。
有可用的使用,而不是默认的会话cookie多种实施方式:
public class CookieTempDataProvider : ITempDataProvider
{
const string CookieName = "TempData";
public void SaveTempData(
ControllerContext controllerContext,
IDictionary<string, object> values)
{
// convert the temp data dictionary into json
string value = Serialize(values);
// compress the json (it really helps)
var bytes = Compress(value);
// sign and encrypt the data via the asp.net machine key
value = Protect(bytes);
// issue the cookie
IssueCookie(controllerContext, value);
}
public IDictionary<string, object> LoadTempData(
ControllerContext controllerContext)
{
// get the cookie
var value = GetCookieValue(controllerContext);
// verify and decrypt the value via the asp.net machine key
var bytes = Unprotect(value);
// decompress to json
value = Decompress(bytes);
// convert the json back to a dictionary
return Deserialize(value);
}
...
参考。 http://brockallen.com/2012/06/11/cookie-based-tempdata-provider/
但是,在请求结束后,这些方法似乎都没有删除cookie。
在请求完成后(除非使用TempData.Keep("myKey");
),是不是使用TempData过期数据的要点?
为什么不使用cookie而不是实现ITempDataProvider?有什么区别/好处?
延伸阅读:
下面是一个简单的基于cookie的实现:
下面是微软的落实SessionState的供应商:
编辑澄清:在下面的代码,微软移除会话被加载后,使得它不能被再次装入:
// If we got it from Session, remove it so that no other request gets it
session.Remove(TempDataSessionStateKey);
在您的实施中删除cookie有什么问题? – 2015-02-05 18:20:55
嗯,我有点好奇,为什么没有发现其他的实现我删除了cookie。我预计Cookie将被删除,就像微软将删除会话一样;然而,这并非我在上面引用的Brock Allen的代码中找到的。 – user1477388 2015-02-05 18:26:54