

Site Context for ApiControllers | Alan Coates – Sitecore/.NET blog
source link: https://blog.coates.dk/2021/05/03/site-context-for-apicontrollers/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Site Context for ApiControllers
Now almost every Sitecore project has rest API’s and I am always shocked when the database, language etc. is hard coded and or additional configuration is added to define default the language, database etc.
Wouldn’t it be nice if you can define the site context for the controller?
Then you can define a site declaration, and or use an existing site that each controller should use and then use the language, database etc. that is define for the site language, database etc.
Solution
The SiteContextAttribute provides the ability to define which site an ApiController should use, for example in the picture above it is setup to use the “Person” site.
public class SiteContextAttribute : ActionFilterAttribute
{
protected readonly string SiteName;
public SiteContextAttribute(string siteName)
{
this.SiteName = siteName;
}
private SiteContextSwitcher _siteContextSwitcher;
private LanguageSwitcher _languageSwitcher;
private DatabaseSwitcher _databaseSwitcher;
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
var siteContext = SiteContext.GetSite(this.SiteName);
_siteContextSwitcher = new SiteContextSwitcher(siteContext);
_databaseSwitcher = new DatabaseSwitcher(siteContext.Database);
_languageSwitcher = new LanguageSwitcher(LanguageManager.GetLanguage(siteContext.Language));
}
public override void OnActionExecuted(HttpActionExecutedContext actionContext)
{
_languageSwitcher?.Dispose();
_databaseSwitcher?.Dispose();
_siteContextSwitcher?.Dispose();
base.OnActionExecuted(actionContext);
}
}
The code gets the site name, then gets the site context and setups the language, database and site context for the controller.
For example, see below it is it possible to use the Context.Database and also the language of the item will also be correct.
I hope this helps, Alan
Recommend
-
12
In this post I will show how Azure Durable functions can complement your sitecore solution and help enhance performance. Problem We took over a Sitecore solution and its content management server was running very slowly and in...
-
16
Untangling the Sitecore Search LINQ to SolR queries
-
17
How IQueryable and Take can kill your Sitecore Solution Leave a reply We had a soluti...
-
10
Introduce a (SolR) Sitecore Search Abstraction
-
6
Ben Coates The Netherlands and beyond Skip to cont...
-
5
Where did my request body data go? Leave a reply Problem The solution had several AP...
-
9
Azure Functions – Json: Self referencing loop detected for property Leave a reply ...
-
5
Sitecore config disappeared? Leave a reply We had a Sitecore 9 solution that required the include file...
-
4
Modified Microphone 2022-Feb-18 I've owned a
-
7
DC says that ‘if’ Ta-Nehisi Coates’ Superman movie is made, it’s going to be an Elseworlds / James Gunn says that Ta-Nehisi Coates and J.J. Abrams’ Superman movie might still happen, and if it does, it’ll exist in its...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK