我正在从episerver 11迁移到12,在视图中有这样的问题:
在所有的块中,在@Html.PropertyFor的@部分中,总是有这样的感觉:
Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer[2]
Connection ID "17293822592188153887", Request ID "80000020-0005-f000-b63f-84710c7967bb": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
at EPiServer.Web.HttpContextExtensions.ContentRenderingContext(HttpContext httpContext)
at EPiServer.Web.Mvc.Html.Internal.DeferredHtmlContent..ctor(Action`1 action, HttpContext httpContext)
at EPiServer.Web.Mvc.Html.PropertyRenderer.GetHtmlForDefaultMode[TModel,TValue](String propertyName, String templateName, String elementName, String elementCssClass, Func`2 displayForAction)
at EPiServer.Web.Mvc.Html.PropertyRenderer.PropertyFor[TModel,TValue](IHtmlHelper`1 html, String viewModelPropertyName, Object additionalViewData, Object editorSettings, Expression`1 expression, Func`2 displayForAction)
at EPiServer.Web.Mvc.Html.PropertyExtensions.PropertyFor[TModel,TValue](IHtmlHelper`1 html, Expression`1 expression)
at AspNetCore.Areas_Main_Views_Shared_Layouts__BaseHtmlBlock.ExecuteAsync() in *MY VIEW NAME*我已经调试了反编译代码,并且在ContentRenderingContext HttpContextAccessor中存在,但是里面的HttpContext是空的。
当我试图在块控制器中注入IHttpContextAccessor时,它具有HttpContext null。
但是-如果我试图在页面控制器和页面视图(@Html.PropertyFor)中获得它,它可以很好地工作,a和HttpContext存在。
在Startup.cs中,它的应用如下:
services.AddHttpContextAccessor();怎么啦?我还需要做些什么吗?
编辑:如果我这样做,那么错误解决,但这看起来不太好,我不想复制粘贴它到处。
款次:
@section Footer {
@{
ServiceLocator.Current.GetInstance<IHttpContextAccessor>().HttpContext = Context;
}
@Html.PropertyFor(x => x.CurrentPage.FooterBottomContentArea)
}对于块(在InvokeComponent中注入InvokeComponent之后):
HttpContextAccessor.HttpContext = HttpContext;发布于 2022-05-18 13:54:59
我们推测这个问题在新版本的episerver中得到了解决,我们从12.3升级到12.6,PropertyFor现在无处不在。
我对它进行了分解,我发现在GetHtmlForDefaultMode中,它在最新版本中使用WrappedHtmlContent,而根本不使用HttpContextAccessor。
https://stackoverflow.com/questions/72260667
复制相似问题