HttpContextAccessor.HttpContext is null in blocks and in @sections

Vote:
 

Hello, I'm doing migration from episerver 11 to 12 and in views have this problem:

In all blocks and and in @section on @Html.PropertyFor always get this exeption:

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*

I've debugged the decompiled code and in ContentRenderingContext HttpContextAccessor exists but the HttpContext inside it is null.

When I try to inject IHttpContextAccessor in block controller it has HttpContext null.

BUT - if i try to get it in page controller and page view (@Html.PropertyFor) it works fine a and HttpContext exists.

In Startup.cs i have it applied like this:

services.AddCms().AddHttpContextAccessor();

What is wrong? Do I have to do anything else?

#280374
May 16, 2022 11:04
Vote:
 

I looked up how ProperyFor is used in the Alloy template project for Episerver 12 and I see that PropertyFor is used only for pages. Blocks use Html.EditAttributes helper. Why? ProperyFor doesn't work for the blocks now? It's not a part of breaking changes for 12 as far as I can see.

I tried replacing Html.EditAttributes with ProperyFor and it works fine.

I laso decompiled it and I see that in GetHtmlForDefaultMode it uses WrappedHtmlContent and doesn't use HttpContextAccessor at all. It used to be (in 12.3 at least) DeferredHtmlContent that used HttpContextAccessor. I'll try to update the veresion up to 12.6 now and see what happens...

#280464
May 17, 2022 14:22
Vote:
 

Updating to 12.6 solved it, PropertyFor works everywhere now.

#280543
May 18, 2022 14:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.