Multi-tenant URL with razor pages

Salvatore Sanollo 25 Reputation points
2024-01-29T20:35:24.9333333+00:00

Hi everyone,
I'm developing a project with multi-tenant razorpages.
To set the various urls in this format "/{tenant}/index"
I wrote this in program.cs:

// Add services to the container.

Now if I write the url containing the tenant (for example /tenant1/index) it works correctly,
but when starting the project I get a 404 (because the startup url is "/").

How do I view a page if there is no tenant?
Then for each project url I am forced to write this:
asp-route-slug="@Context.Request.RouteValues["slug"]"

Is there a better way to not pass the slug parameter?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,796 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ruikai Feng - MSFT 2,756 Reputation points Microsoft External Staff
    2024-01-30T03:16:33.5066667+00:00

    Hi,@Salvatore Sanollo You may try :

    builder.Services.AddRazorPages()
         .AddRazorPagesOptions(options =>
         {
             options.Conventions.AddFolderRouteModelConvention("/", model =>
             {
                 model.Selectors.Remove(model.Selectors.FirstOrDefault(x => x.AttributeRouteModel.Template == ""));
                 foreach (var selector in model.Selectors)
                 {
                     if (!selector.AttributeRouteModel.Template.Contains("welcome"))
                     {
                         selector.AttributeRouteModel = new AttributeRouteModel
                         {
                             Order = -1,
                             Template = AttributeRouteModel.CombineTemplates(
                             "{slug}",
                             selector.AttributeRouteModel.Template),
                         };
                     }                 
                 }
                 
             });
         });
    .....
    
    var app = builder.Build();
    ......
    
    
    app.MapFallbackToPage("/welcome");
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. Best regards, Ruikai Feng

    0 comments No comments

  2. Salvatore Sanollo 25 Reputation points
    2024-01-30T06:52:54.7366667+00:00

    How do I display a specific page if there is no tenant? With your code the application starts but points to the webapp page. I would like to display a welcome page


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.