OnCurrentSite() and OnSite(CMS.SiteProvider.SiteContext.CurrentSiteName) give System.NullReferenceEx

Predit Regmi asked on October 4, 2019 04:47

This is my first Kentico Project. Using Kentico 12 MVC. I am trying to use different sites for staging dev -> uat -> prod. And hence for queries I am trying to get documents of the current site. But whenever I use OnCurrentSite() or OnSite() it gives

"System.NullReferenceException: Object reference not set to an instance of an object. CMS.DocumentEngine.DocumentQueryBase`2.OnSite(SiteInfoIdentifier siteIdentifier, Boolean includeGlobal) +11".

I tried in IIS as well as Azure with the same result. Without the OnCurrentSite() or OnSite() I get the documents but it includes the document from all the sites which is not useful.

I am trying something simple like below.

      return CMS.DocumentEngine.DocumentHelper.GetDocuments<WhatWeDo>()
                .OnSite(CMS.SiteProvider.SiteContext.CurrentSiteName)
                .AddColumns("Logo" , "HeadingLine1", "HeadingLine2",)
                .TopN(1)
                .ToList()
                .Select(m => new WhatWeDoDto()
                {
                    Logo = m.Logo,
                    HeadingLine1 = m.HeadingLine1,
                    HeadingLine2 = m.HeadingLine2,
                })
                .FirstOrDefault();

Here are the results from SiteContext and HttpContext

var cSiteId = SiteContext.CurrentSiteID;
var cSite = SiteContext.CurrentSite;
var cName = CMS.SiteProvider.SiteContext.CurrentSiteName;
var host = HttpContext.Current.Request.Url.Host;
var domain = System.Web.HttpRuntime.AppDomainAppPath;
var sInfo = SiteInfoProvider.GetRunningSiteInfo(domain,host);

Result:

cSiteId 0   
cSite   null
cName   ""
host    "localhost"
domain  "C:\\Users\\abc\\Documents\\Name\\AzureDevops\\site\\site-DEV\\site\\"
sInfo   null

Any help would be appreciated.

Thank you

Recent Answers


David te Kloese answered on October 4, 2019 10:27

Hi,

Where is this code being called? As it seem the request context hasn't been initialized (yet). Since you are talking about multiple sites and environments I assume it's a running project so it must also work in other sections of your site. Perhaps try to find other document retrieval methods and see where they are located.

0 votesVote for this answer Mark as a Correct answer

Eric Dugre answered on October 4, 2019 21:05

In MVC, a null reference to the current site generally occurs because the MVC site's domain doesn't match the one defined in the site's Presentation URL in Kentico. Can you double-check that the URL you are accessing the MVC front-end matches the Presentation URL?

1 votesVote for this answer Mark as a Correct answer

Predit Regmi answered on October 7, 2019 02:34

Thank you for the replies.

The code is being called in the repository, which is being called by controller. All of the document retrieval methods currently do not have OnCurrentSite() or OnSite().


The admin portals are running on the following:

  • websiteadmin.azurewebsites.net
  • websiteadmin-dev.azurewebsites.net
  • websiteadmin-uat.azurewebsites.net

And the mvc sites are running as follows:

  • websitemvc.azurewebsites.net
  • websitemvc-dev.azurewebsites.net
  • websitemvc-uat.azurewebsites.net

So, lets say I am using websiteadmin.azurewebsites.net and websitemvc.azurewebsites.net. The current domain name is: websiteadmin.azurewebsites.net and the presentation URL is: websitemvc.azurewebsites.net

I checked the presentation URL and it matches the URL I am trying to access. I did try changing domain and presentation url to match but I still get the same error.

0 votesVote for this answer Mark as a Correct answer

Daniel Nistreanu answered on October 31, 2019 07:26

Happens for me too.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on October 31, 2019 14:16

Any specific hotfix version?

0 votesVote for this answer Mark as a Correct answer

Christian Nickel answered on March 10, 2020 18:57

I realize this is an older question, but in addition to setting the "Presentation URL" we had to add the domains to the "Domain Aliases" for the site, after that we restarted the site and it started working.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.