skip to Main Content

We have a asp.net core website using Razor without controller and I want to set default page for every sub folder.

For example, the root folder is Pages, then we have PagesMembers folder, PagesProducts folder. While someone is visiting http://www.xxx.xxx/Products, I want it to show PagesProductsDefault.cshtml page.

I have tried

app.MapRazorPages();
app.UseDefaultFiles();

It does not work well. How can I implement it? Thanks!

2

Answers


  1. The default page in each subfolder is Index.cshtml.

    To set a different page see: How to configure razorpages to not use Index.cshtml as the default page in folders under the Pages folder

    Login or Signup to reply.
  2. You can use an IPageRouteModelConvention (https://www.learnrazorpages.com/advanced/custom-route-conventions) to centralise this without having to remember to apply a route template to each page.

    Create a class that implements IPageRouteModelConvention which enables you to create a convention for generating routes at start up in addition to the default conventions provided by the framework:

    public class HomePageRouteModelConvention : IPageRouteModelConvention
    {
        public void Apply(PageRouteModel model)
        {
    
            foreach (var selector in model.Selectors.ToList())
            {
                var template = selector.AttributeRouteModel.Template;
                if (template.Contains("/"))
                {
                    // is a folder
                    var segments = template.Split(new[] { '/' }, StringSplitOptions.None);
    
                    // check to see if this is the route for the Default page
                    if(segments.Last().ToLowerInvariant() == "default")
                    {
                        var folderPath = $"{segments[0]}/";
                        for (var i = 1; i < segments.Length - 1; i++)
                        {
                            folderPath += $"{segments[i]}/";
                        }
                        // if it is, add another route template that points to the folder without the page name
                        model.Selectors.Add(
                            new SelectorModel
                            {
                                AttributeRouteModel = new AttributeRouteModel
                                {
                                    Template = folderPath
                                }
                            });
                    }
                }
            }
        }
    }
    

    Register this with the application services:

    builder.Services.AddRazorPages().AddRazorPagesOptions(options =>
    {
        options.Conventions.Add(new HomePageRouteModelConvention());
    });
    

    Depending on your circumstances, you might also need to check that there isn’t an Index page in the same folder. If there is, you will create ambiguous routes for the folder and the framework won’t know which page to call, unless you remove the default template from the Index page’s templates.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search