C# 当照片不存在时,UserManager.GetUserAsync(User).Result.ProfilePicture失败

C# 当照片不存在时,UserManager.GetUserAsync(User).Result.ProfilePicture失败,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我为用户添加了将照片作为头像添加到个人资料中的功能,但遇到了一个问题。然后,我添加了一个进程,在用户登录时显示照片。但是,当用户创建新帐户时,我遇到了一种情况。如果用户创建了一个帐户,然后尝试登录,则登录失败。在这种情况下,获取照片的查询似乎不处理空值 我试图先检查照片,只是为了确定,但代码在检查步骤中失败 @if (UserManager.GetUserAsync(User).Result.ProfilePicture.Length &g

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

我试图先检查照片,只是为了确定,但代码在检查步骤中失败

                            @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>
                            }
@if(UserManager.GetUserAsync(用户).Result.ProfilePicture.Length>0&&UserManager.GetUserAsync(用户).Result.ProfilePicture!=null)
{
  • }
    这就是生成的错误。如何安全地检查照片并仅显示一张照片(如果存在),否则,在没有照片的情况下优雅地显示菜单

    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)
    
    处理请求时发生未处理的异常。
    NullReferenceException:对象引用未设置为对象的实例。
    AspNetCore.Views\u Shared\u Layout+c\u DisplayClass54\u 0+d.MoveNext()位于\u Layout.cshtml第102行
    堆栈查询Cookies头路由
    NullReferenceException:对象引用未设置为对象的实例。
    AspNetCore.Views\u Shared\u Layout+c\u DisplayClass54\u 0+d.MoveNext()在\u Layout.cshtml中
    +
    @if(UserManager.GetUserAsync(User).Result.ProfilePicture.Length>0&&UserManager.GetUserAsync(User).Result.ProfilePicture!=null)
    Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
    \u Layout.cshtml中的AspNetCore.Views\u Shared\u Layout.ExecuteAsync()
    +
    var stats=“活动”;
    Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAscync(IRazorPage页面,ViewContext上下文)
    Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage页面,ViewContext上下文,bool invokeViewStarts)
    Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext上下文,ViewBufferTextWriter bodyWriter)
    Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext上下文)
    Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext-ViewContext,string-contentType,可空状态码)
    Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext-ViewContext,string-contentType,可空状态码)
    Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext ActionContext、IView视图、ViewDataDictionary viewData、ITempDataDictionary tempData、string contentType、可空状态代码)
    Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext上下文,ViewResult结果)
    Microsoft.AspNetCore.Mvc.ViewResult.ExecuteSultAsync(ActionContext上下文)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_|29_0(ResourceInvoker invoker,Task lastTask,State next,Scope Scope,object State,bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed上下文)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext(ref状态next、ref范围、ref对象状态、ref bool已完成)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_|24_0(ResourceInvoker invoker,Task lastTask,State next,Scope Scope,object State,bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed上下文)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref状态Next、ref作用域、ref对象状态、ref bool已完成)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u Logged|17|u 1(ResourceInvoker invoker)
    Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext HttpContext)
    Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext上下文)
    Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext上下文)
    Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext上下文)
    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文)
    
    在检查是否为空之前,您正在评估
    ProfilePicture
    的长度。所以

    更改:

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

    如果您使用的是最新版本的C#//Razor,我建议您执行以下操作:

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

    如果你知道空值在哪里,你可以处理。如果ProfilePicture为空,则此UserManager.GetUserAsync(User).Result.ProfilePicture.Length将为您提供异常。交换第一个if中的两个条件可能会解决这个问题。如果没有,请单独执行各个步骤,或者查看调试中的数据,以确定可以安全引用的内容。
    @if (UserManager.GetUserAsync(User)?.Result?.ProfilePicture != null && UserManager.GetUserAsync(User)?.Result?.ProfilePicture?.Length > 0)