2013-12-11 48 views
1

我在我的global.asax.cs Application Start中使用了Http.Current.Response。當我在我的電腦中執行時,它工作正常,沒有任何問題。如何,當我嘗試把它與Windows Server 2008 R2的IIS,我發現它給出了以下錯誤。 enter image description here「System.Web.HttpException:響應在此上下文中不可用」僅在IIS中

它的代碼是

public static void SetCookie(string key, string value, int dayExpires) 
    { 
     HttpCookie encodedCookie = HttpSecureCookie.Encode(new HttpCookie(key, value)); 
     encodedCookie.Expires = DateTime.Now.AddDays(dayExpires); 

     HttpContext.Current.Response.Cookies.Remove(key); 
     HttpContext.Current.Response.Cookies.Add(encodedCookie); 
    } 

我想描繪出它爲什麼在我的系統得到執行,而不是在IIS。

感謝

+0

這個叫什麼時候? –

+0

它被稱爲Application_Start() –

回答

4

我不會做的請求/響應導向的東西的Application_Start。試試BeginRequest

+0

謝謝,我會試試 –

3

當在IIS7中以集成模式運行時,請求上下文在應用程序啓動時不可用。

請參閱我的問題和接受的答案查看詳情:

Global ASAX - get the server name


還要注意,似乎是在你的代碼的邏輯錯誤 - 這將設置cookie只爲在應用程序啓動時點擊該網站的人 - 這不會針對每個請求或每個會話運行。

+0

謝謝,我會試試看 –

相關問題