umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.49k stars 2.69k forks source link

UmbracoContext.PublishedRequest is NULL in module view since upgrading to 13.3.2 from 13.3.0 #16426

Open stefanie27 opened 5 months ago

stefanie27 commented 5 months ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.3.2

Bug summary

I'm using UmbracoContext.PublishedRequest.PublishedContent within module views (i.e. UmbracoCms\Views\Partials\BlockList\default.cshtml) to get the current page. Since upgrading from 13.3.0 to 13.3.2 this error started occurring and is caused by UmbracoContext.PublishedRequest being null

Specifics

NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Views_Partials_blocklist_default.ExecuteAsync() in default.cshtml

Steps to reproduce

Add following line of code in any module view, i.e. UmbracoCms\Views\Partials\BlockList\default.cshtml var currentPage = UmbracoContext.PublishedRequest.PublishedContent; When the page with module is loading, error message "NullReferenceException: Object reference not set to an instance of an object." is displayed

Expected result / actual result

No response

github-actions[bot] commented 5 months ago

Hi there @stefanie27!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

nikolajlauridsen commented 5 months ago

Hello @stefanie27 I've tried reproducing your issue however I'm unable to, when I try to drop the following into both UmbracoCms\Views\Partials\BlockGrid\default.cshtml and UmbracoCms\Views\Partials\BlockGrid\Components\umbBlockGridDemoHeadlineBlock.cshtml and both works just fine.

@{
    var currentPage = UmbracoContext?.PublishedRequest?.PublishedContent;

    if (currentPage is not null)
    {
        <h2>Page: @currentPage.Name</h2>
    }
}

However this seems like it may be a routing thing, so to be able to look more into this I would need to know how the page you're hitting (where the blocklist/grid is rendered) is routed, is it just completely normal piece of umbraco content, or is there some custom controller behind it? And if so, how does that controller look?

stefanie27 commented 1 week ago

FYI If anyone has the same issue. Issue happens on pages with custom route and FindContent. I updated my code in View as following:

var currentPage = UmbracoContext.PublishedRequest?.PublishedContent;
if (currentPage == null)
{
    currentPage = Context.Features.Get<Umbraco.Cms.Web.Common.Routing.UmbracoRouteValues>()?.PublishedRequest?.PublishedContent;
}