[英]UserManager.GetUserAsync(User).Result.ProfilePicture Failing when Photo does not exists
I added the ability for users to add a photo to their profile as an avatar and ran into a problem.我为用户添加了将照片作为头像添加到他们的个人资料中的功能,但遇到了问题。 I then added a process to show the photo when the user logs in. However, I ran into a situation when a User is creating a new account.
然后,我添加了一个在用户登录时显示照片的过程。但是,当用户创建新帐户时,我遇到了一种情况。 If a User creates an account and then tried to login, the Login failed.
如果用户创建帐户然后尝试登录,则登录失败。 It appears that the Query to get the photo does not handle nulls in this case.
在这种情况下,获取照片的查询似乎不处理空值。
I tried to check for the photo first, just to be sure, but the code is failing in that check step.为了确定,我先尝试检查照片,但代码在该检查步骤中失败。
@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>
}
This is the Error that is generated.这是生成的错误。 How do I safely check for the photo and only show one if it eixists, otherwise, gracefully display the menu without it?
我如何安全地检查照片并仅在它存在时才显示一张,否则,在没有它的情况下优雅地显示菜单?
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)
You're evaluating the length of ProfilePicture
before you're checking if it's null.在检查它是否为 null 之前,您正在评估
ProfilePicture
的长度。 So,所以,
Change:改变:
@if (UserManager.GetUserAsync(User).Result.ProfilePicture.Length > 0 && UserManager.GetUserAsync(User).Result.ProfilePicture != null)
To:至:
@if (UserManager.GetUserAsync(User).Result.ProfilePicture != null && UserManager.GetUserAsync(User).Result.ProfilePicture.Length > 0)
If you're using the latest version of C#//Razor, I'd recommend doing:如果您使用的是最新版本的 C#//Razor,我建议您这样做:
@if (UserManager.GetUserAsync(User)?.Result?.ProfilePicture != null && UserManager.GetUserAsync(User)?.Result?.ProfilePicture?.Length > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.