簡體   English   中英

當照片不存在時,UserManager.GetUserAsync(User).Result.ProfilePicture 失敗

[英]UserManager.GetUserAsync(User).Result.ProfilePicture Failing when Photo does not exists

我為用戶添加了將照片作為頭像添加到他們的個人資料中的功能,但遇到了問題。 然后,我添加了一個在用戶登錄時顯示照片的過程。但是,當用戶創建新帳戶時,我遇到了一種情況。 如果用戶創建帳戶然后嘗試登錄,則登錄失敗。 在這種情況下,獲取照片的查詢似乎不處理空值。

為了確定,我先嘗試檢查照片,但代碼在該檢查步驟中失敗。

                            @if (UserManager.GetUserAsync(User).Result.ProfilePicture.Length > 0 && UserManager.GetUserAsync(User).Result.ProfilePicture != null)
                            {
                                <li class="nav-link" style="align-self: center;">
                                    <img style="width:40px;height:40px; object-fit:cover; border-radius:30px;margin-right:-30px;" src="data:image/*;base64,@(Convert.ToBase64String(UserManager.GetUserAsync(User).Result.ProfilePicture))">
                                </li>
                            }

這是生成的錯誤。 我如何安全地檢查照片並僅在它存在時才顯示一張,否則,在沒有它的情況下優雅地顯示菜單?

An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Shared__Layout+<>c__DisplayClass54_0+<<ExecuteAsync>b__1>d.MoveNext() in _Layout.cshtml, line 102

Stack Query Cookies Headers Routing
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Shared__Layout+<>c__DisplayClass54_0+<<ExecuteAsync>b__1>d.MoveNext() in _Layout.cshtml
+
                            @if (UserManager.GetUserAsync(User).Result.ProfilePicture.Length > 0 && UserManager.GetUserAsync(User).Result.ProfilePicture != null)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
AspNetCore.Views_Shared__Layout.ExecuteAsync() in _Layout.cshtml
+
    var stats = "active";
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext context, ViewBufferTextWriter bodyWriter)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

在檢查它是否為 null 之前,您正在評估ProfilePicture的長度。 所以,

改變:

@if (UserManager.GetUserAsync(User).Result.ProfilePicture.Length > 0 && UserManager.GetUserAsync(User).Result.ProfilePicture != null)

至:

@if (UserManager.GetUserAsync(User).Result.ProfilePicture != null && UserManager.GetUserAsync(User).Result.ProfilePicture.Length > 0)

如果您使用的是最新版本的 C#//Razor,我建議您這樣做:

@if (UserManager.GetUserAsync(User)?.Result?.ProfilePicture != null && UserManager.GetUserAsync(User)?.Result?.ProfilePicture?.Length > 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM