2013-06-21 27 views
0

我遇到了將Dictionary對象中的數據傳遞給自定義ExtendedMembershipProvider中的CreateUserAndAccount方法的問題。在我的帳戶控制器時,(郵政)註冊方法有以下幾點:MVC 4將字典轉換爲RouteValueDictionary

Dictionary<string, object> userInfo = new Dictionary<string, object>(); 

userInfo.Add("Email", model.Email); 
userInfo.Add("PasswordQuestion", model.PasswordQuestion); 
userInfo.Add("PasswordAnswer", model.PasswordAnswer); 

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, userInfo, true); 

其中填充用戶信息,併成功地調用我的自定義提供的CreateUserAndAccount方法。

我有兩個 - 也許連接 - 問題。

首先,方法簽名是不一樣的,供應商的方法是這樣的:

public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary<string, object> values) 

布爾和字典參數切換,但仍達到了方法。如果我更改了帳戶/註冊方法的代碼以符合本我得到:

爲 WebMatrix.WebData.WebSecurity.CreateUserAndAccount的最佳重載的方法匹配(字符串,字符串, 對象,布爾)」有一些無效的參數。

我很困惑,這是怎麼發生的,我的問題基本上是什麼?

其次,當代碼到達CreateUserAndAccount時,我傳遞給它的Dictionary對象已轉換爲RouteValueDictionary,所有其他參數都按預期顯示。

如何獲取我的Dictionary對象並訪問Email,PasswordQuestion和PasswordAnswer值?

回答

1

the static WebSecurity.CreateUserAndAccount method的簽名與the ExtendedMembershipProvider.CreateUserAndAccount method的簽名不匹配。你不能用另一個的簽名來調用一個方法,這就是爲什麼當你嘗試時你會得到編譯器錯誤。

WebSecurity類中的方法propertyValues參數顯式轉換爲RouteValueDictionary,因爲它的目的是接受任何對象,而ExtendedMembershipProvider方法需要一個IDictionary<string, object>參數。

例如,你可以傳遞一個匿名對象,並調用將仍起作用:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, 
    new { model.Email, model.PasswordQuestion, model.PasswordAnswer }, 
    true); 
+0

感謝理查德,偉大的答案。 TBH我應該發現,自己,長編碼會議結束... –