Reputation: 4325
I've implemented Web API 2 with a Kentico 9 CMS platform (generally following Kentico's documentation) and on the whole it's working well. However, in my controllers I'm finding SiteContext.CurrentSite
always returns null. Is this expected behaviour or am I doing something wrong?
Not sure if it makes a difference but the platform hosts multiple sites under a single domain as per this documentation. So my sites are running at the following URLs...
...and the API is available under the following paths...
Currently I'm having to use the Request object to pull out the request path and check this against the sites from Kentico's SiteProvider
to find which Kentico site the current request relates to, but I wouldn't have though that necessary since the API is running as part of the site.
Is there something I can do to wire this up properly? FYI I've noticed that LocalizationContext.CurrentCulture
does return a CultureInfo
object so it's not as though I'm completely disconnected from Kentico's context...
UPDATE WITH SOLUTION
Thanks to @martin-makarsky for the answer below. Used this to create the following extension method which can be called from controller using Request.GetCurrentSite()
public static SiteInfo GetCurrentSite(this HttpRequestMessage request)
{
return SiteInfoProvider.GetRunningSiteInfo(request.RequestUri.Host, System.Web.HttpRuntime.AppDomainAppPath);
}
Upvotes: 2
Views: 996
Reputation: 2650
Well, nobody answered this question so I`ll give it a try. SiteContext uses internally HttpContext.Current
which is little bit 'unreliable' in web api due to some thread and async related stuff (does your action contain await?). This could explain your nulls
when calling SiteContext.CurrentSite
in your controller.
So back to your problem - you need to get current SiteInfo
inside you app (probably web api controller or service or repository) - this means you likely know your current domain or appPath. I would try to use something from SiteInfoProvider
like GetRunningSiteInfo(string domainName, string applicationPath)
to get current SiteInfo
.
Good luck!
Upvotes: 5