A
A
alexkr_net2019-04-12 18:51:25
ASP.NET
alexkr_net, 2019-04-12 18:51:25

Prefix route in Razor Pages: how to organize link generation?

I have the following problem: I need to create two different routes in Razor Pages. The first - standard, the second - with a prefix for example simple.
I do it this way: I implement the IPageRouteModelConvention interface and add my template with the simple prefix

public class SimplePageRouteModelConvention : 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
                {
                    Template = AttributeRouteModel.CombineTemplates("simple", selector.AttributeRouteModel.Template)
                }
            });
        }
    }
}

Everything works fine, both sitename/info and sitename/simple/info. Both routes lead to the info.cshtml file.
The problem lies in the generated links. Each link is prefixed with the simple prefix we added in the route. Besides, it doesn't matter if I refer to a page without simple sitename/info or with simple sitename/simple/info , links always contain simple.
I can't figure out how I can get links to be generated depending on which route I use to access the page.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question