所有整數或布爾值,如何申報(全球?)變量作爲在WebMatrix中C#
我只是想知道是否有每次不重新分配變量頁頁次後勤的方式在頁面加載,但仍然沒有得到未被選中的變量YSOD屏幕。
我想在每次加載頁面時增加pageCount,以便強制「history.go(pageCount * -1)」(在JavaScript中)。
如果我將pageCount分配爲0(例如,int pageCount = 0;),則每次頁面加載時都會將其重置爲0,但如果我在分支中初始化它,則隱藏範圍輸入字段值在C#中繪製(JavaScript可用於拾取另一側)。
我曾嘗試屆時AppState,但它不喜歡爲int的工作(或布爾爲此事),即使我嘗試投它喜歡:
(int)AppState["pageCount"];
我如何可以聲明任何建議全局變量(缺少一個更好的單詞)變量爲int或bool,或者應用邏輯來聲明一個常規變量,該變量在每次頁面加載時都不會重置爲初始值?
var username = "";
var user = "";
var password = "";
var errorMessage = "";
var loginSuccess = "not";
AppState["gActionMessage"] = "";
int pageCount;
if(!IsPost)
{
pageCount = 1;
}
if(IsPost)
{
pageCount++;
username = Request.Form["username"];
password = Request.Form["password"];
if(username.IsEmpty() || password.IsEmpty())
{
errorMessage = "You must specify both a username and password.";
}
else
{
if (WebSecurity.UserExists(username) && WebSecurity.GetPasswordFailuresSinceLastSuccess(username) > 4 && WebSecurity.GetLastPasswordFailureDate(username).AddSeconds(120) > DateTime.UtcNow) {
AppState["gActionMessage"] = "You're account has been locked due to too many failed login attempts. " +
"Please try again in 2 minutes.";
AppState["gActionMessageDisplayed"] = "not";
Response.Redirect("~/");
return;
}
if(WebSecurity.Login(username, password, false))
{
var db = Database.Open("Users");
user = (db.QuerySingle("SELECT firstName FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", username)).firstName;
AppState["gActionMessage"] = "Hello, " + user + "!";
AppState["gActionMessageDisplayed"] = "not";
loginSuccess = "is";
//Response.Redirect("~/");
}
else
{
errorMessage = "Login failed. Please check your user name and password for errors.";
loginSuccess = "not";
}
}
}
上述代碼錯誤在第7行,由於pageCount未被分配。
感謝您的幫助!
AppState是爲整個網站,並會影響所有用戶。也許你想爲個人用戶提供會話(有點像AppState)? – Knox
你能舉個例子嗎? – VoidKing
我想我必須改變我的所有AppStates到會話......大聲笑 – VoidKing