gpt4 book ai didi

c# - 发布到 azure 时出现 System.NullReferenceException

转载 作者:行者123 更新时间:2023-12-03 01:53:30 26 4
gpt4 key购买 nike

您好,我已经发布了我的网站,该网站可以在本地从 Visual Studio fork 到 Azure,现在每次我尝试访问该页面时都会遇到以下异常。

[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,我将在其中发布代码。

角色操作

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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9efdfff0dbfaf7eacbedfbecdee9f7f0f9eaf7eeeaf1e7edb0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>",
Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8cbc9c6edccc1dcfddbcddae8dfc1c6cfdcc1d8dcc7d1db86cbc7c5" rel="noreferrer noopener nofollow">[email protected]</a>"
};
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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7112101f34151805240214033106181f16051801051e08025f121e1c" rel="noreferrer noopener nofollow">[email protected]</a>").Id, "canEdit"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("ca<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e507b5a574a6b4d5b4c7e495750594a574e4a51474d105d5153" rel="noreferrer noopener nofollow">[email protected]</a>").Id, "canEdit");
}

if (!userMgr.IsInRole(userMgr.FindByEmail("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cba0aab8a2fdff8bb8aeb1a5aaa6e5a8b1" rel="noreferrer noopener nofollow">[email protected]</a>").Id, "canEdit"))
{
IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b101a08124d4f3b081e01151a16551801" rel="noreferrer noopener nofollow">[email protected]</a>").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();



}
}
}

我不明白为什么它在本地运行良好,但发布时却出现问题。谢谢

最佳答案

NullReferenceException 很可能是由 userMgr.FindByEmail("xxx") 调用之一引起的。您不处理 userMgr.FindByEmail 方法返回 null 的情况,而是直接访问它的 Id 属性。

关于c# - 发布到 azure 时出现 System.NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30492147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com