Multiple App.Razor components

Kuler Master 406 Reputation points
2025-01-31T08:47:54.43+00:00

Hello,

Is it possible to have an additional App.razor file that I can use for e.g. Admin area only?

Currently, if I create a new folder named Admin and create a new layout there, it inherits from the existing App.razor and linked css files.

Basically, I need to have separate css files in the admin area.

Thank you!

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-01-31T16:47:33.2266667+00:00

    Blazor use a single component tree. The app component is the root node, of which there can only be one. As for css, there is only one browser instance, so css by definition is shared with all components. Blazor components support “isolation” css by prefixing all class references with a unique selector.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-9.0

    I assume you want admin to look like a new page. In that case the admin pages should reference their own layout.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/layouts?view=aspnetcore-9.0#apply-a-default-layout-to-an-app

    0 comments No comments

  2. Anonymous
    2025-02-03T08:25:04.54+00:00

    Is it possible to have an additional App.razor file that I can use for e.g. Admin area only?

    No

    You may try load css file based on area in app.razor

    @if (.....)
        {
            <link rel="stylesheet" href="......."/>
            
        }
        else
        {
            <link rel="stylesheet" href="......."/>
        }
    
    .....
    
    @code{
        
        [CascadingParameter]
        HttpContext? context{ get; set; }
    
    }
    
    
    

    when you navigate to components in another area,you need a full page reload


    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

  3. Mario Viadero Hidalgo 0 Reputation points
    2025-02-27T07:15:10.35+00:00

    Hello, I have a similar problem where we have two distinct parts of the application with different dependencies and initialization modules.

    It is a Webapp project using Interactive WebAssembly render mode, and we cannot use another mode due to incompatibility with our component suite.

    Our issue is not related to downloading WASM files but rather to the startup process of the application and the initialization of modules. Additionally, once initialized, if the user is not authenticated and gets redirected to Azure B2C, when they return to the Blazor application, it has to initialize again, causing significant delays.

    We considered a similar approach: having two App.razor components—one for authenticated users that initializes all modules and another for users without permissions that only initializes the login-related components.

    If this is not possible, would it be feasible for the Webapp project (Blazor and Blazor.Client) to reference another Blazor project (Blazor.Client2) without all the dependencies included in the main one?

    Best regards, and thanks.

    0 comments No comments

  4. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-02-27T18:49:34.4333333+00:00

    your use case is not clear. blazor already supports authenticated and non authenticated access to pages and content display, why two apps? also authentication login is an unload of the blazor app, redirect to login page, reload of the blazor app.


  5. Jack Dang (WICLOUD CORPORATION) 1,020 Reputation points Microsoft External Staff
    2025-07-09T08:04:03.75+00:00

    Hi,

    Regarding your question, you can use custom routing and layout logic to simulate multiple entry points.

    Here's a sample project:

    Project Structure

    Pages/
    │
    ├── Admin/
    │   └── Dashboard.razor
    Shared/
    │
    └── AdminLayout.razor
    wwwroot/
    │
    ├── css/
         └── admin.css
    

    Create these files as above.

    AdminLayout.razor:

    @inherits LayoutComponentBase
     
    <link href="css/admin.css" rel="stylesheet" />
     
    <div class="admin-layout">
    <h1>Admin Panel</h1>
        @Body
    </div>
    

    Dashboard.razor:

    @page "/admin/dashboard"
    @using YourProjectName.Shared // You can find it in Program.cs
    @layout AdminLayout
     
    <h3>Welcome to the Admin Dashboard</h3>
     
    <p>This page uses a separate layout and CSS.</p>
    

    admin.css:

    .admin-layout {
        background-color: #222;
        color: white;
        padding: 20px;
    }
    

    Run the App:

    Press F5 or click Run.

    Navigate to https://localhost:port/admin/dashboard to see the Admin layout in action.

    Hope my solution was helpful.


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.