2015-05-27 24 views
1

你好,我已經發布了我的網站,它是從Visual Studio本地分叉到Azure,現在每當我嘗試訪問該頁面時,我都會得到以下異常。System.NullReferenceException發佈到天藍

[NullReferenceException: Object reference not set to an instance of an object.] 
    WebApplication1.Logic.RoleActions.AddUserAndRole() +390 
    WebApplication1.Global.Application_Start(Object sender, EventArgs e) +47 

因爲它指向Logic.RoleActions和Global.Application_Start,我會在這些代碼中發佈代碼。

RoleActions

namespace WebApplication1.Logic 
{ 
internal class RoleActions 
{ 
    internal void AddUserAndRole() 
    { 
     // Access the application context and create result variables. 
     Models.ApplicationDbContext context = new ApplicationDbContext(); 
     IdentityResult IdRoleResult; 
     IdentityResult IdUserResult; 

     // Create a RoleStore object by using the ApplicationDbContext object. 
     // The RoleStore is only allowed to contain IdentityRole objects. 
     var roleStore = new RoleStore<IdentityRole>(context); 

     // Create a RoleManager object that is only allowed to contain IdentityRole objects. 
     // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. 
     var roleMgr = new RoleManager<IdentityRole>(roleStore); 

     // Then, you create the "canEdit" role if it doesn't already exist. 
     if (!roleMgr.RoleExists("canEdit")) 
     { 
      IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" }); 
     } 

     // Create a UserManager object based on the UserStore object and the ApplicationDbContext 
     // object. Note that you can create new objects and use them as parameters in 
     // a single line of code, rather than using multiple lines of code, as you did 
     // for the RoleManager object. 
     var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); 
     var appUser = new ApplicationUser 
     { 
      UserName = "[email protected]", 
      Email = "[email protected]" 
     }; 
     IdUserResult = userMgr.Create(appUser, "Pa$$word1"); 

     // If the new "canEdit" user was successfully created, 
     // add the "canEdit" user to the "canEdit" role. 
     if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit")) 
     { 
      IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"); 
     } 

     if (!userMgr.IsInRole(userMgr.FindByEmail("[email protected]").Id, "canEdit")) 
     { 
      IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("[email protected]").Id, "canEdit"); 
     } 
    } 
} 
} 

和全球

namespace WebApplication1 
{ 
public class Global : HttpApplication 
{ 
    void Application_Start(object sender, EventArgs e) 
     { 
      // Code that runs on application startup 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 

      // Create the custom role and user. 
      RoleActions roleActions = new RoleActions(); 
      roleActions.AddUserAndRole(); 



     } 
    } 
} 

我不明白爲什麼它本地工作正常,但當公佈了一個問題。謝謝

+0

可能重複[什麼是一個NullReferenceException,如何解決呢?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i -fix-it) –

回答

1

NullReferenceException很可能是由userMgr.FindByEmail("xxx")調用之一引起的。您不處理userMgr.FindByEmail方法返回null的情況,並且您直接訪問它的Id屬性。

1

我懷疑這是因爲沒有用戶在Azure中使用[email protected]的電子郵件。因此,在這一行調用Id將拋出一個空引用異常。的

userMgr.FindByEmail("[email protected]").Id 
+0

是的,它就是這樣。 –