2016-02-03 44 views
1

我正在遷移一個網站,以使用MVC 6.目前我有tempdata存儲在cookie中,但我無法找到如何在新的MVC中做到這一點的設置框架。ITempDataProvider在MVC 6使用餅乾tempdata

+0

我遇到同樣的問題。你解決了這個問題嗎? – ctorx

+0

不,我從來沒有做過 –

+0

找到它了,答案在下面。如上所述,刪除cookie有點不可靠,我不確定它是否是beta版問題或其他問題。我想與你分享我在這個問題上的成功,所以如果你解決了cookie問題,請告訴我們。 – ctorx

回答

-1

如果您考慮用於存儲下一個請求的數據的TempData類,那麼MVC 6中會有一些更改。您需要添加其他包並對其進行配置。以下是步驟:

  1. 從[project.json]中的框架部分刪除「dnxcore50」。 Session尚未在dnxcore50中實現。

  2. 在[project.json]添加:

    "Microsoft.AspNet.Session": "1.0.0-rc1-final" 
    
  3. 啓用類緩存和會話Startup.cs,方法ConfigureServices,通過添加下一個線 services.AddMvc( ):

    services.AddCaching(); 
    services.AddSession(); 
    
  4. Cinfigure它類星tup.cs,方法配置,加上下一行以前 app.UseMvc(...):

    app.UseSession(); 
    

就是這樣。但請記住,您只能存儲基本或可序列化的數據類型。如果您需要存儲用戶定義的數據類型,則需要對其進行序列化。爲此,我們使用「Newtonsoft.Json」庫。下面是例子:

string json = JsonConvert.SerializeObject(myObject); 
TempData["myKey"] = json; 
+0

謝謝,但我正在考慮將臨時數據保存在Cookie中 –

0

首先,實現您的ITempDataProvider。我這樣做,使用JSON.Net。

public class CookieTempDataProvider : ITempDataProvider 
{ 
    readonly string CookieKey = "_tempdata"; 

    public IDictionary<string,object> LoadTempData(HttpContext context) 
    { 
     var cookieValue = context.Request.Cookies[this.CookieKey]; 
     if(string.IsNullOrWhiteSpace(cookieValue)) 
     { 
      return new Dictionary<string, object>(); 
     } 

     var decoded = Convert.FromBase64String(cookieValue); 
     var jsonAsString = Encoding.UTF8.GetString(decoded);    
     var dictionary = JsonConvert.DeserializeObject<IDictionary<string,object>>(jsonAsString, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Full }); 

     // The cookie really should be deleted when the SaveTempData() method is called with an empty dictionary 
     // but that does not seem to be working for some reason. Added this delete for now (maybe this is a beta issue) 
     // TODO: Revisit at next release 
     context.Response.Cookies.Delete(this.CookieKey); 

     return dictionary; 
    } 

    public void SaveTempData(HttpContext context, IDictionary<string,object> values) 
    { 
     if (values == null || values.Count == 0) 
     { 
      context.Response.OnStarting(() => Task.Run(() => 
      { 
       context.Response.Cookies.Delete(this.CookieKey); 
      })); 

      return; 
     } 

     var jsonAsString = JsonConvert.SerializeObject(values, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Full }); 
     var bytes = Encoding.UTF8.GetBytes(jsonAsString); 
     var encoded = Convert.ToBase64String(bytes); 

     context.Response.Cookies.Append(this.CookieKey, encoded); 
    }   
} 

接下來,在Startup.cs,其中服務是有線起來,用你的自定義版本替換默認ITempDataProvider,就像這樣:

public void ConfigureServices(IServiceCollection services) 
{ 
    // Replace Temp Data Provider 
    var existing = services.FirstOrDefault(x => x.ServiceType == typeof(ITempDataProvider)); 
    services.Remove(existing); 
    services.AddSingleton<ITempDataProvider, CookieTempDataProvider>(); 
} 

編輯

由於RC2原由於MVC請求生命週期中的時序變化,答案不再起作用......您將收到有關無法修改標題的錯誤。我已經更新了上面的SaveTempData()方法來解決這個問題。

0

我也有這個需求,所以我實現了一個基於cookie的TempData供應商爲ASP.NET核心MVC和發佈它在NuGet。它可用here