prefix razor pages in razor class library

61 Views Asked by At

I was wondering if there is an option to prefix the route of razor pages inside a razor class library?

I want to create a blazor application which includes other modules each module should also have their own rcl, now i was looking if i could prefix all the pages inside a razor class library without the need to manually change all the @page "" routes by myself.

once you run the blazor application you go to localhost:5000 from their you can go to localhost:5000/Customers from here you are in de customers module if you want to go to details the url should be localhost:5000/Customers/Details/1 for example

so the @page in details is set to @page "Customers/Details/{id}" is their a possibility that Customers is always included for pages in this rcl?

2

There are 2 best solutions below

0
Alamakanambra On

I am not sure there is a way how to achieve that without a change, but there is a way how to do it clean:

Instead of @page "/Customers/Details/{Id}" you can use @attribute [Route("/Customers/Details/{Id}")], which also means you can use string interpolation:

@attribute [Route($"{Constants.ModulePrefix}/Details/{Id}")]
0
Ruikai Feng On

You could try move your pages into a folder and try AddFolderRouteModelConvention method

A minimal example:

builder.Services.AddRazorPages
(op =>
    {
        op.Conventions.AddFolderRouteModelConvention("/MyFolder", model =>
        {
            foreach (var selector in model.Selectors)
            {
                var c = selector.AttributeRouteModel.Template.ToString();
                selector.AttributeRouteModel = new AttributeRouteModel
                {
                    Order = -1,
                    Template = c.Substring(c.IndexOf('/') + 1) + "/" + "Prefix"

                };
            }
        });
});

enter image description here

It works well on myside:

enter image description here