我正在尝试使用索赔身份asp.net创建用户,但在创建索赔身份用户时收到此错误。
ApplicationUser user = new ApplicationUser {
EmailConfirmed = true,
UserName = model.myUser.Email,
Email = model.myUser.Email ,
PhoneNumber = model.myUser.PhoneNumber,
PhoneNumberConfirmed = true,
UserImagePath = model.myUser.UserImagePath,
FirstName= model.myUser.FirstName,
LastName = model.myUser.LastName,
DateOfBirth = model.myUser.DateOfBirth,
Culture = model.myUser.Culture,
Role = model.myUser.Role
};
但是当代码是
var user= new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
它工作得很好,所以我想知道出什么问题了
您在发布的代码之前有一条语句(例如if或while),没有花括号。
例如:
if (somethingIsTrue)
{
var user= new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
}
是正确的,但是下面的代码:
if (somethingIsTrue)
var user = new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
将导致CS1023:嵌入式语句不能是声明或带标签的语句。
根据@codefrenzy,原因是新声明的变量将立即超出范围,除非将其包含在可以从中访问它的块语句中。
但是,编译将在以下情况下通过。
如果您仅初始化类型的新实例,而不声明新变量:
if (somethingIsTrue)
new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
或者,如果您为现有变量分配值:
ApplicationUser user;
if (somethingIsTrue)
user = new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句