2015-09-25 15 views
3

我試圖讓我的在asp.net MVC應用程序的用戶的一些信息與Visual Studio 2015年System.Web.HttpContext.Current不存在

當我嘗試從請求獲取信息與

System.Web.HttpContext.Current req = new System.Web.HttpContext.Current(); 

我得到一個錯誤Error CS0426 The type name 'Current' does not exist in the type 'HttpContext'

任何人都知道如何解決這一問題?

+0

'Current'是一個屬性,而不是一個方法。 –

+1

...一個靜態屬性,而不是一個類型。 –

+0

'var req = new System.Web.HttpContext.Current;' – Igor

回答

7

如果你想獲得當前上下文

System.Web.HttpContext currentContext = System.Web.HttpContext.Current; 

如果你想創建一個(出於某種原因,比如測試)

System.Web.HttpContext newContext = new System.Web.HttpContext(
    new System.Web.HttpRequest("", "http://example.com", ""), 
    new System.Web.HttpResponse(new System.IO.StringWriter()) 
    ); 
+0

呃,不,不是答案。不可能,因爲不管什麼深不可測的原因,Current不存在於HttpContext中。所以你不能只是去'System.Web.HttpContext currentContext = System.Web.HttpContext.Current'因爲當前不存在。我不知道爲什麼它不存在,這是這裏的問題。 –

-2

您可以設置這樣的會話變量:

System.Web.HttpContext.Current.Request.ServerVariables["country_code"]