You see a lot of CultureInfo.CurrentUICulture and CultureInfo.CurrentCulture references in the Accelerator. Those are connected to two channel fields called Language for pages and block and Language for products. Lets have a look at when to use which!
Language for pages and blocks and language for products on a channel
Language for pages and block sets the value for CultureInfo.CurrentUICulture. Language for products sets the value for CultureInfo.CurrentCulture. This means you can do like this when getting a value for a field that is multiculture for proucts:
And for blocks and pages you instead get field values like this:
In the Litium projects I’ve worked these fields mentioned above are almost always pointing to the same language. So it often times won’t make much difference. However, if you only use CultureInfo.CurrentCulture when working with products and CultureInfo.CurrentUICulture when working with pages and blocks you’ll be future proof if an editor decides to change those fields.
To use a Middleware with Litium we can create a startup class that inherits the IOwinStartupConfiguration interface and use it to insert a middleware into the OWIN pipeline.
A middleware is, put simply, a piece of code which can read information about the current request and add information to the response. And it’s a good place to put code that doesn’t really fit into controllers or services where we need to examine every request.
The example below will examine every request that comes into a Litium site. We are looking for a specific querystring and we want to store its value as a cookie. This is a real life example, but it has been modified and simplified.
This is tested with Litium 5 and 7 but probably works in more versions. For the sake of simplicity this post will not take any project standards into consideration. But feel free to use the comments to suggest changes or improvements.
1. Creating a startup class
Since I’m using a fresh install of the Accelerator for demonstration purposes and not doing this in a customer project I’m just going to put this class in the Litium.Accelerator.Mvc root folder. Your project structure will probably be different depending on your project standards.
Start by adding a new class called Startup.cs and let this class inherit and implement IOwinStartupConfiguration. Then we would have a class that looks like this:
2. Creating a middleware
Create a new folder in the Litium.Accelerator project called Middleware and create a class called UtmCampaign.cs. Let this class inherit from OwinMiddleWare and implement that abstract class. (You need to install the nuget package Microsoft.Owin).
The UtmCampaign.cs class will now look like this:
3. Adding logic to the middleware
For this example I want to save the campaign name sent by different ad campaigns into a cookie. (I could then use this value when communicating with other third party services, which I have done for some customers, but we won’t cover that in this tutorial).
The UtmCapaign.cs middleware will now look like this:
4. Adding the middleware to the owin pipline
Lets go back to Startup.cs and add the middleware we just created to the owin pipeline like this:
And if I now browse to http://accelerator.localtest.me/?utm_campaign=asdf I can see that my middleware is put to use and has created a cookie for me:
Litium middleware result
5. Other uses of IOwinStartupConfiguration
The IOwinStartupConfiguration class isn’t only to used register your own middlewares. You can also use it to, for example, setup signalR if you want to use that on your Litium site.
There are two folders where Litium stores files. The Files Directory and the Common Files directory. Both contains uploaded images, thumbnails, and search indices etc. But the Common files directory is used in multiserver environments, and it stores information that servers would share.
You can set these paths in web.config. By default the Accelerator v 7.2.3 has the filesDirectory set to ..\Files. I do not want it in the project folder but somewhere else on my disk. It is up to you where you want to put it. Do not forget to add it to your .gitignore(or exclude it from your version control) if you keep it in the default folder.
Lets change this:
To this: D:\filefolders\accelerator
(Don’t forget to set permissions to allow the site to save files to it)
To set a commonfilesDirectory you can add it below the filesDirectory in web.config
Lets take a look at using Papercut as SMTP-server during web site development. We’ll also take a look at StateServer. These tools works for any ASP.NET MVC-project but this post is a continuation of the Installing Litium Accelerator post.
Papercut will act as an SMTP-server and will display every email sent from the website we are building. This is how the creators of Papercut explain it:
Papercut is a 2-in-1 quick email viewer AND built-in SMTP server (designed to receive messages only). Not only does it not enforce any restrictions how you prepare your email, but it allows you to view the whole email-chilada: body, html, headers, attachment down right down to the naughty raw encoded bits. Papercut can be configured to run on startup and sit quietly (minimized in the tray) only providing a notification when a new message has arrived.
Now open up web.config of your project and change this:
Next start your site (mine is accelerator.localtest.me) and log in to Litium. Once logged in go to Settings -> Websites -> Websites. Then edit your site and update the Sender email address to an address of your choice.
Litium - Sender email address
Save the settings and order something from the accelerator and you will see an order confirmation email show up in Papercut.
Litium - Sender email address
2. Using StateServer sessionstate
By default the site is setup to use InProc mode. This is fine, But every time the site gets restarted the sessiondata will be lost and we loose information about the cart for example. If we change this to StateServer we save some time every time we rebuild the project since the cart will be saved in our StateServer instead.
Open web.config and change this:
Then start the services app and start the ASP.NET State Service.
At this point I assume you’ve installed the IIS and the .NET framework, the nuget feed, and has created an account that has got download rights for the accelerator. (Linked to in the System Requirements above).
Go to Litium docs - Download and download the accelerator. If you have access to it the package will show up at the top at Latest accelerators. At the time the latest version is Litium Accelerator 7.2.3.
Extract the .zip into a folder where you want to work with the solution.
2. Install via Visual Studio
Open Accelerator.sln after extracting the package.
Go to View -> Other Windows -> Package Manager Console and run the command Update-Package -ReInstall
Wait for install to complete (This took around 3 minutes for me).
Running Update-Package -ReInstall
If you, like me, got an error saying: “Update-Package : Some NuGet packages are missing from the solution. The packages need to be restored in order to build the dependency graph. Restore the packages before performing any operations” Make sure you’ve set up the nuget feed correctly and click the restore button that has shown up at the top of the package manager console. (I had recently changed passwords to my account and needed to update it to get it to work)
3. Configuring the Accelerator
Now right click on the Litium.Accelerator.Mvc-project in the Solution Explorer and click on Set as startup project and run the project.
First run of Litium Accelerator
When the site has started we are redirected to the login page. Login with your windows account (ex: DOMAIN\[email protected] or .\Crille)
Then click on the Cogwheel (top right) -> Deployment -> Accelerator
Select a name and a domain name for the site. I’m using accelerator.localtest.me in this case. By choosing a localtest.me-domain I do not need to add anything to the hosts-file.
Check the Create a set of example products and categories checkbox and the click Import. This will give us some example data.
4. Moving database to Sql Server
By default the accelerator uses a LocalDb instance. Lets move that db to a MSSQLServer instance.
Open Microsoft SQL Server Management Studio as an administrator then right click on Databases and select Attach…
Then click on Add… and select the .mdf -file that is located in the Litium.Accelerator.Mvc/App_Data folder.
Then click ok and attach the database.
When this is done you need to update the FoundationConnectionString in web.config to point to your MSSQL-Instance instead (Not covered here).
5. Adding an IIS site
Next fire up the IIS and right click on Sites and then on Add Website… and select a name.
Point the Physical path to \Src\Litium.Accelerator.Mvc and add the hostname you selected. (in my case http://accelerator.localtest.me/)
You might need to run the Application pool as your local user to get access to the database. But I’ll leave the iis-configuration up to you.
Now browse to the site and you should end up with something like this: