How to get SiteCode Name in StartUp.cs class

Chandra Bhattarai asked on May 26, 2022 13:07

I am looking the site code name for some level of configuration. I am using Kentico 13 core (13.68). After getting null, i initialize the cms application as below but even i am getting null in SiteContext.

public void Configure(IApplicationBuilder app) { CMSApplication.Init(); var siteName=SiteContext.CurrentSiteName.ToLower() ; .......................

Recent Answers


Sean Wright answered on June 9, 2022 05:29

You are not using ASP.NET Core or Kentico Xperience's API correctly. The Configure method (which I touch on in my blog post Kentico Xperience Design Patterns: Good Startup.cs Hygiene ) is used for defining the middleware pipeline that ASP.NET Core uses to process a request.

There are no guarantees about a request being processed at that point in the application (the pipeline needs to be defined before any requests can be handled).

Kentico Xperience requires an active request to populate SiteContext because it uses the request's URL to identify the request's associated site. Without an active request, there's no value populating SiteContext.

You should either be using ASP.NET Core middleware or an MVC Controller to process a request.

Also, you shouldn't use static global context objects like SiteContext in ASP.NET Core. Instead use dependency injection and access the current site through ISiteService.CurrentSite.

0 votesVote for this answer Mark as a Correct answer

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