ASP.Net MVC 使用文档将默认字符串 ID 更改为和 int,但出现错误
ASP.Net MVC change default string ID to and int using docs get error tho
我做了一个基本的 ASP.Net MVC Web 项目。
我使用 Microsoft 文档将所有 ID 从 string
切换为 int
。
我做了这些更改 + MVC 更新 3 更改。
当我编译 运行 我的新项目时,它在我的文件中显示为 int
,但我收到错误消息,无法完成 运行 项目。
这是我的消息在输出中显示的内容:
Exception thrown: 'System.FormatException' in mscorlib.dll
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Input string was not in a correct format.
如有任何意见,我们将不胜感激。我做了 运行 迁移,它显示为 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
谢谢。
回答我的问题。
在我的startup.auth.cs
我遵循了我之前所说的所有 Microsoft 文档。
我在 运行 我的项目中一直遇到错误。
在 CookieAuth 下的 ConfigureAuth() 中添加 CultureInfo 作为第二个参数。
这是我完成的项目代码。
{
// 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(),
CultureInfo.InvariantCulture)
//getUserIdCallback:(id) => (Int32.Parse(id.GetUserId()))
)
}
因此,为了回答您的问题,Parse
方法使用当前语言环境将您的字符串转换为整数。我猜想在您的情况下, GetUserID()
将采用您当前的语言环境期望逗号作为小数点的格式。因此,提供可选参数 CultureInfo.InvariantCulture
会使您的语言环境不区分文化。另一种方法是提供第二个可选参数以提供 IFormatProvider.
要解决您的错误,请将此行设置为:
(id)=>Int32.Parse(id.GetUserId(),CultureInfo.InvariantCulture)
有关 CultureInfo.InvariantCulture 的更多信息。
干杯。
我做了一个基本的 ASP.Net MVC Web 项目。
我使用 Microsoft 文档将所有 ID 从 string
切换为 int
。
我做了这些更改 + MVC 更新 3 更改。
当我编译 运行 我的新项目时,它在我的文件中显示为 int
,但我收到错误消息,无法完成 运行 项目。
这是我的消息在输出中显示的内容:
Exception thrown: 'System.FormatException' in mscorlib.dll An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Input string was not in a correct format.
如有任何意见,我们将不胜感激。我做了 运行 迁移,它显示为 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
谢谢。
回答我的问题。
在我的startup.auth.cs
我遵循了我之前所说的所有 Microsoft 文档。
我在 运行 我的项目中一直遇到错误。
在 CookieAuth 下的 ConfigureAuth() 中添加 CultureInfo 作为第二个参数。
这是我完成的项目代码。
{
// 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(),
CultureInfo.InvariantCulture)
//getUserIdCallback:(id) => (Int32.Parse(id.GetUserId()))
)
}
因此,为了回答您的问题,Parse
方法使用当前语言环境将您的字符串转换为整数。我猜想在您的情况下, GetUserID()
将采用您当前的语言环境期望逗号作为小数点的格式。因此,提供可选参数 CultureInfo.InvariantCulture
会使您的语言环境不区分文化。另一种方法是提供第二个可选参数以提供 IFormatProvider.
要解决您的错误,请将此行设置为:
(id)=>Int32.Parse(id.GetUserId(),CultureInfo.InvariantCulture)
有关 CultureInfo.InvariantCulture 的更多信息。
干杯。