首页 > 解决方案 > 当照片不存在时,UserManager.GetUserAsync(User).Result.ProfilePicture 失败

问题描述

我为用户添加了将照片作为头像添加到他们的个人资料中的功能,但遇到了问题。然后,我添加了一个在用户登录时显示照片的过程。但是,当用户创建新帐户时,我遇到了一种情况。如果用户创建帐户然后尝试登录,则登录失败。在这种情况下,获取照片的查询似乎不处理空值。

为了确定,我先尝试检查照片,但代码在该检查步骤中失败。

                            @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)

标签: c#asp.net-mvcasp.net-core

解决方案


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)

推荐阅读