我刚刚将我的应用程序更新到 .NET Core 3.1 并将流式验证更新到最新版本 (8.6.2),并且在呈现具有输入字段的视图组件时出现以下错误。
System.NullReferenceException: Object reference not set to an instance of an object.
at FluentValidation.AspNetCore.ValidatorDescriptorCache.GetCachedDescriptor(ClientValidatorProviderContext context, IHttpContextAccessor httpContextAccessor) in
/home/jskinner/code/FluentValidation/src/FluentValidation.AspNetCore/ValidatorDescriptorCache.cs:line 45
at FluentValidation.AspNetCore.FluentValidationClientModelValidatorProvider.CreateValidators(ClientValidatorProviderContext context) in \/home\/jskinner\/code\/FluentValidation\/src\/FluentValidation.AspNetCore\/FluentValidationClientModelValidatorProvider.cs:line 73
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.CompositeClientModelValidatorProvider.CreateValidators(ClientValidatorProviderContext context)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ClientValidatorCache.GetValidators(ModelMetadata metadata, IClientModelValidatorProvider validatorProvider)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultValidationHtmlAttributeProvider.AddValidationAttributes(ViewContext viewContext, ModelExplorer modelExplorer, IDictionary`2 attributes)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ValidationHtmlAttributeProvider.AddAndTrackValidationAttributes(ViewContext viewContext, ModelExplorer modelExplorer, String expression, IDictionary`2 attributes)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.AddValidationAttributes(ViewContext viewContext, TagBuilder tagBuilder, ModelExplorer modelExplorer, String expression)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateInput(ViewContext viewContext, InputType inputType, ModelExplorer modelExplorer, String expression, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, String format, IDictionary`2 htmlAttributes)
at Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateTextBox(ViewContext viewContext, ModelExplorer modelExplorer, String expression, Object value, String format, Object htmlAttributes)
at Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.GenerateHidden(ModelExplorer modelExplorer, IDictionary`2 htmlAttributes) at Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.Process(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
at AspNetCore.Views_Waardecheck_SubscribeToEmail_SubscribeToEmail_Unsubscribed.<ExecuteAsync>b__11_0() in
问题是在视图组件上,HttpContext
实例IHttpContextAccessor
的 是null
,因此当流利的验证尝试将验证器插入管道时会给出空引用异常。我确实services.AddHttpContextAccessor();
在Startup
.
这是受影响的观点之一。如果我删除该字段中的asp-for
标签助手,input
那么它就可以工作。但这并不是真正的解决方案。
<div data-subscribe-to-email class="waardecheck-subscribe-to-email waardecheck-subscribe-to-email--unsubscribed fd-bg-light-3 fd-padding-horizontal fd-padding-vertical-s fd-justify-content-between fd-border-light-2 fd-border-horizontal-remove fd-border-bottom-remove">
<p class="fd-margin-none fd-margin-bottom-xs">@WaardecheckResources.EnableUpdatesDescription</p>
<form asp-action="UpdateEmailPreferences">
<input asp-for="SelectedEmailFrequency" value="@Model.SelectedEmailFrequency" type="hidden">
<button type="submit" class="fd-btn fd-btn--primary-alt fd-btn--block"><span class="icon-alert-white"></span> Enable</button>
</form>
</div>
有没有人遇到过同样的问题,或者您对我该如何解决这个问题有任何想法?