首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HttpContextAccessor.HttpContext在块中和@节中为空

HttpContextAccessor.HttpContext在块中和@节中为空
EN

Stack Overflow用户
提问于 2022-05-16 14:16:28
回答 1查看 230关注 0票数 1

我正在从episerver 11迁移到12,在视图中有这样的问题:

在所有的块中,在@Html.PropertyFor的@部分中,总是有这样的感觉:

代码语言:javascript
运行
复制
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中,它的应用如下:

代码语言:javascript
运行
复制
services.AddHttpContextAccessor();

怎么啦?我还需要做些什么吗?

编辑:如果我这样做,那么错误解决,但这看起来不太好,我不想复制粘贴它到处。

款次:

代码语言:javascript
运行
复制
@section Footer {
    @{
        ServiceLocator.Current.GetInstance<IHttpContextAccessor>().HttpContext = Context;
    }
    
    @Html.PropertyFor(x => x.CurrentPage.FooterBottomContentArea)
}

对于块(在InvokeComponent中注入InvokeComponent之后):

代码语言:javascript
运行
复制
HttpContextAccessor.HttpContext = HttpContext;
EN

Stack Overflow用户

回答已采纳

发布于 2022-05-18 13:54:59

我们推测这个问题在新版本的episerver中得到了解决,我们从12.3升级到12.6,PropertyFor现在无处不在。

我对它进行了分解,我发现在GetHtmlForDefaultMode中,它在最新版本中使用WrappedHtmlContent,而根本不使用HttpContextAccessor。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72260667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档