C# MVC4将字典转换为RouteValueDictionary
我在将Dictionary对象中的数据传递给自定义ExtendedMembershipProvider中的CreateUserAndAccount方法时遇到问题。在我的帐户控制器中,(Post)注册方法具有以下功能:C# MVC4将字典转换为RouteValueDictionary,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,我在将Dictionary对象中的数据传递给自定义ExtendedMembershipProvider中的CreateUserAndAccount方法时遇到问题。在我的帐户控制器中,(Post)注册方法具有以下功能: Dictionary<string, object> userInfo = new Dictionary<string, object>(); userInfo.Add("Email", model.Email); userInfo.Add("Passwo
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);
Dictionary userInfo=newdictionary();
userInfo.Add(“Email”,model.Email);
userInfo.Add(“PasswordQuestion”,model.PasswordQuestion);
userInfo.Add(“PasswordAnswer”,model.PasswordAnswer);
WebSecurity.CreateUserAndAccount(model.UserName、model.Password、userInfo、true);
它填充userInfo并成功调用自定义提供程序中的CreateUserAndAccount方法
我有两个问题,可能是相关的
首先,方法签名不同,提供者方法如下:
public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary<string, object> values)
public重写字符串CreateUserAndAccount(字符串用户名、字符串密码、布尔要求确认、IDictionary值)
Boolean和Dictionary参数已切换,但仍然可以使用该方法。如果我更改Account/Register方法中的代码以与此匹配,我会得到:
匹配的最佳重载方法
WebMatrix.WebData.WebSecurity.CreateUserAndAccount(字符串,字符串,
对象,bool)“”具有一些无效参数
我很困惑这是怎么发生的,我的问题是到底发生了什么
其次,当代码到达CreateUserAndAccount时,我传递给它的Dictionary对象已转换为RouteValueDictionary,所有其他参数都会按预期显示
如何将Dictionary对象取回并访问电子邮件、PasswordQuestion和PasswordAnswer值?的签名与的签名不匹配。不能使用另一个方法的签名调用一个方法,这就是为什么在尝试时会出现编译器错误
WebSecurity
类上的方法显式地将propertyvalue
参数转换为RouteValueDictionary
,因为它被设计为接受任何对象,而ExtendedMembershipProvider
方法需要一个IDictionary
参数
例如,您可以传入一个匿名对象,调用仍然有效:
WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
new { model.Email, model.PasswordQuestion, model.PasswordAnswer },
true);
谢谢Richard,回答得很好。TBH我应该自己发现,在漫长的编码过程结束后。。。