ASP.Net MVC 使用文档将默认字符串 ID 更改为和 int 获取错误

我做了一个基本的 ASP.Net MVC Web 项目。我使用 Microsoft 文档将我的所有 ID 从string转换为一个。int


https://learn.microsoft.com/en-us/aspnet/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity#run


我做了这些更改 + MVC Update 3 更改。当我编译并运行我的新项目时,它int在我的文件中显示为一个,但我收到错误并且无法完成运行该项目。


这是我的消息在输出中显示的内容:


抛出的异常:mscorlib.dll 中的“System.FormatException” mscorlib.dll 中出现“System.FormatException”类型的异常,但未在用户代码中处理 输入字符串的格式不正确。


任何输入将不胜感激。我确实运行了迁移,它显示为int但没有在我的浏览器中完全运行。


app.UseCookieAuthentication(new CookieAuthenticationOptions

            {

                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,

                LoginPath = new PathString("/Account/Login"),

                Provider = new CookieAuthenticationProvider

                {

                    // Enables the application to validate the security stamp when the user logs in.

                    // This is a security feature which is used when you change a password or add an external login to your account.  

                    OnValidateIdentity = SecurityStampValidator

                    .OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(

                        validateInterval: TimeSpan.FromMinutes(30),

                        regenerateIdentityCallback: (manager, user) => 

                        user.GenerateUserIdentityAsync(manager), 

                        getUserIdCallback:(id)=>Int32.Parse(id.GetUserId()))

                }

            });

我的代码在这部分中断了,那就是异常 id.GetUserId()


这是在 startup.auth.cs


谢谢你。


蝴蝶刀刀
浏览 150回答 2
2回答

吃鸡游戏

因此,为了回答您的问题,该Parse方法使用当前语言环境将您的字符串转换为整数。我猜在您的情况下,这GetUserID()将采用您当前语言环境期望逗号作为小数点的格式。因此,提供可选参数CultureInfo.InvariantCulture将使您的区域设置文化不敏感。另一种方法是提供第二个可选参数来提供IFormatProvider。要解决您的错误,请将此行设置为:(id)=>Int32.Parse(id.GetUserId(),CultureInfo.InvariantCulture)有关CultureInfo.InvariantCulture的更多信息。

跃然一笑

回答我的问题。在我的 startup.auth.cs如前所述,我遵循了所有 microsoft 文档。运行项目时,我不断收到错误消息。在 CookieAuth 下的 ConfigureAuth() 中添加 CultureInfo 作为第二个参数。这是我完成的项目代码。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Enables the application to validate the security stamp when the user logs in.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is a security feature which is used when you change a password or add an external login to your account.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnValidateIdentity = SecurityStampValidator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validateInterval: TimeSpan.FromMinutes(30),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; regenerateIdentityCallback: (manager, user) =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.GenerateUserIdentityAsync(manager),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getUserIdCallback:(id) => Int32.Parse(id.GetUserId(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CultureInfo.InvariantCulture)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //getUserIdCallback:(id) => (Int32.Parse(id.GetUserId()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP