亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP.Net MVC 使用文檔將默認字符串 ID 更改為和 int 獲取錯誤

ASP.Net MVC 使用文檔將默認字符串 ID 更改為和 int 獲取錯誤

C#
蝴蝶刀刀 2022-11-13 13:58:45
我做了一個基本的 ASP.Net MVC Web 項目。我使用 Microsoft 文檔將我的所有 ID 從string轉換為一個。inthttps://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謝謝你。
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

因此,為了回答您的問題,該Parse方法使用當前語言環境將您的字符串轉換為整數。我猜在您的情況下,這GetUserID()將采用您當前語言環境期望逗號作為小數點的格式。因此,提供可選參數CultureInfo.InvariantCulture將使您的區域設置文化不敏感。另一種方法是提供第二個可選參數來提供IFormatProvider

要解決您的錯誤,請將此行設置為:

(id)=>Int32.Parse(id.GetUserId(),CultureInfo.InvariantCulture)

有關CultureInfo.InvariantCulture的更多信息。


查看完整回答
反對 回復 2022-11-13
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

回答我的問題。


在我的 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()))

                    )

                }


查看完整回答
反對 回復 2022-11-13
  • 2 回答
  • 0 關注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號