Configure Culture Route Model

In a public website, route value is the best option for localization because people must be able to share page links for any selected culture. There is more options like query string parameter, cookies and accepted page header value, but we will focus on route value.

Define Culture Route Model Convention

We want our culture parameter to be next to the web application Url and before any other route parameter. e.g. -->

Create a new class inside "Utilities" folder and name it “CultureTemplateRouteModelConvention.cs

new class dialog

This class will implement the IPageRouteModelConvention interface:

CultureTemplateRouteModelConvention.cs :
using Microsoft.AspNetCore.Mvc.ApplicationModels;

namespace MyTrips.Utilities
    /// Configure {culture?} as first route parameter in the request path
    public class CultureTemplateRouteModelConvention : IPageRouteModelConvention
        public void Apply(PageRouteModel model)
            var selectorCount = model.Selectors.Count;
            for (var i = 0; i < selectorCount; i++)
                var selector = model.Selectors[i];
                model.Selectors.Add(new SelectorModel
                    AttributeRouteModel = new AttributeRouteModel
                        Order = -1,
                        Template = AttributeRouteModel.CombineTemplates(

Hint: to insert required/missing references put the cursor on the red underlined code and press (Ctrl + . ) you can use this key combination to insert required references, remove unused references and to implement the interfaces as well.

We have defined a route parameter named {culture?} notice that is has ( ? ) Therefore, it is optional, because URLs are hackable and any user may try to remove or change the culture from the address bar, in case the selected culture not supported, the system must return default provided culture.

After we defined our culture route value template, we need to configure our application to use this template for all pages. In the project root open “startup.cs" file and modify add razor page options:

        .AddRazorPagesOptions(o => {
            o.Conventions.Add(new CultureTemplateRouteModelConvention());

Read more about razor pages route conventions in Microsoft docs here

Our application configured to use culture route template, next we will build the route culture provider that will use the route parameter to localize the request and provide list of supported cultures. Lets test our culture route template, run the project and navigate to any of the below links:

  • https://localhost:xxxx/en/trips
  • https://localhost:xxxx/tr/trips
  • https://localhost:xxxx/ar/trips

All links is showing the same “Trips” page, we will not see any localization info yet, because we need to configure request localization provider then create localization resources.

Next : Localizing Request

Demo Project: My trips application

Source code on github: MyTrips