如何從HttpContext.Current訪問TempData?從HttpContext.Current訪問TempData
回答
您不能/不應該從HttpContext.Current
訪問TempData
。你需要一個控制器實例。不幸的是,因爲你沒有解釋你的情況,爲什麼你需要這樣做,我不能爲你提供一個更好的選擇。
我正在創建一個以更友好/類型安全的方式包裝TempData的類。它在內部使用TempData,但我不想公開這個事實。我不想在像'new MyTempDataWrapper(this.TempData)'中傳遞TempData。如果TempData只存儲在Session中,並且Session可以從HttpContext.Current訪問,爲什麼我不能從HttpContext.Current訪問TempData? – BowserKingKoopa 2011-03-26 20:05:51
將您的評論發送給其他答案,您可以實現自己的ITempDataProvider,然後重寫控制器以使用它。看一下Mvc3Futures中的CookieTempDataProvider類,它將tempdata存儲在cookie而不是會話中,以查看這是如何實現的。
http://volaresystems.com/Blog/post/2011/06/30/Sessionless-MVC-without-losing-TempData.aspx
而不是改變的TempData存儲在何處,您的實現可以從SessionCookieTempDataProvider可能繼承和簡單的添加類型安全的方法吧。
如果由於自己的設計決定而不傳遞上下文對象作爲參數,那麼您至少可以在您自己的全局靜態類上使用[ThreadStatic]。對於Property訪問的成員來說,這可能會很方便,而這些成員又必須依賴這樣的ThreadStatic參數,因爲它們不是函數。
ThreadStatic可以幫助將同一線程上的資源共享到遠處的堆棧幀,而無需傳遞參數。 HttpContext.Current使用ThreadStatic來實現這一點。
一個常規的MVC控制器類不會爲你做這個。因此,您需要爲您的項目中的所有控制器創建自己的類以從中繼承。
public class MyController : Controller
{
public MyController()
{
_Current = this;
}
[ThreadStatic]
public static RacerController _Current = null;
public static RacerController Current
{
get
{
var thisCurrent = _Current; //Only want to do this ThreadStatic lookup once
if (thisCurrent == null)
return null;
var httpContext = System.Web.HttpContext.Current;
if (httpContext == null) //If this is null, then we are not in a request scope - this implementation should be leak-proof.
return null;
return thisCurrent;
}
}
protected override void Dispose(bool disposing)
{
_Current = null;
base.Dispose(disposing);
}
}
用法:
var thisController = MyController.Current; //You should always save to local variable before using - you'll likely need to use it multiple times, and the ThreadStatic lookup isn't as efficient as a normal static field lookup.
var value = thisController.TempData["key"];
thisController.TempData["key2"] = "value2";
- 1. 訪問TempData變量
- 2. 從WCF Web服務訪問HttpContext.Current
- 3. 訪問數據訪問層中的HttpContext.Current
- 4. 訪問到TempData的從Global.asax中
- 5. 如何從tempdata訪問數組元素
- 6. 如何訪問Task.Factory.StartNew中的HttpContext.Current?
- 7. 在定製中間件中訪問TempData
- 8. 在javascript中訪問tempdata在mvc4
- 9. 在ExecuteResult中訪問TempData Asp.Net MVC Core
- 10. 如何從HttpContext.Current
- 11. 設置tempdata從Global.asax
- 12. 如何訪問asp.net中線程中的httpcontext.current或session值
- 13. C#ASP.NET:如何訪問緩存時沒有HttpContext.Current可用(爲空)?
- 14. ashx處理程序,訪問HttpContext.Current裏面的空洞
- 15. 爲什麼通過HttpContext.Current訪問會話不好[練習]?
- 16. TempData的問題鍍鉻
- 17. 使用TempData時的問題
- 18. ASP.Net MVC - TempData會話問題
- 19. Moq'ing HttpContext.Current
- 20. HTTPContext.Current問題 - 我正確使用它嗎?
- 21. HttpContext.Current爲空
- 22. HttpContext.Current VS ActionExecutingContext
- 23. HttpSelfHostServer和HttpContext.Current
- 24. SIGNALR HttpContext.Current爲空
- 25. HttpContext.Current返回null
- 26. Parallel.ForEach錯誤HttpContext.Current
- 27. .NET Remoting和HttpContext.Current
- 28. MultipartFormDataStreamProvider vs HttpContext.Current
- 29. 使用HttpContext.Current(C#)
- 30. TempData Wrapper
你找到解決這個問題呢? – trailmax 2013-10-01 23:54:37