1
votes

Looking for an easy way build multi tenant razor page. Looking for the url pattern to be like \{Tenant}\{Page} for all pages in an area. Its fairly easy to add a route param at the end via RazorPagesOptions Conventions. How do you add the param at the beginning?

1
Similar to github.com/aspnet/Mvc/issues/7779 : However, you should look into Razor Pages Conventions feature as that will enable you to write your own convention and implement the custom routing you're interested in.Nan Yu

1 Answers

4
votes

You can use the IPageRouteModelConvention interface to prefix each route with a route parameter representing the tenant. Create a class that implements the interface, and then override the Apply method, something like the following (untested):

public class CustomPageRouteModelConvention : IPageRouteModelConvention
{
    public void Apply(PageRouteModel model)
    {
        foreach (var selector in model.Selectors.ToList())
        {
            selector.AttributeRouteModel.Template = "{tenant}/" +  selector.AttributeRouteModel.Template ;
        }
    }
}

Then register your implementation in ConfigureServices:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.Add(new CustomPageRouteModelConvention());
})