0

Here I have blazor server app, in app I have two _Host.cshtml, one for website which is default _Host.cshtml and another for admin panel that is _HostAdmin.cshtml. Both of them have separate css and js file. I too have separate layout, for website I have Mainlayout.razor which should use _Host.cshtml and for admin panel I have AdminLayout.razor which should use _HostAdmin.cshtml .

Now, the problem is that when I add new razor component and use AdminLayout.razor layout the view doesnot use css and js of _HostAdmin.cshtml.

Below is my _HostAdmin.cshtml

@page "/Admin"
@namespace MCQ.Pages._Pages_Admin__HostAdmin
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
  <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <title>Admin Dashboard</title>
     <meta name="viewport" content="width=device-width, initial-scale=1">

     <base href="~/Admin" />

     <link rel="stylesheet" href="/Adminlte/plugins/fontawesome-free/css/all.min.css" />
     <link rel="stylesheet" href="/Adminlte/plugins/fontawesome-free/css/all.min.css">
     <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
     <link rel="stylesheet" href="/Adminlte/plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
     <link rel="stylesheet" href="/Adminlte/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
     <link rel="stylesheet" href="/Adminlte/plugins/jqvmap/jqvmap.min.css">
     <link rel="stylesheet" href="/Adminlte/dist/css/adminlte.min.css">
     <link rel="stylesheet" href="/Adminlte/plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
     <link rel="stylesheet" href="/Adminlte/plugins/daterangepicker/daterangepicker.css">
     <link rel="stylesheet" href="/Adminlte/plugins/summernote/summernote-bs4.css">
     <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">

</head>
  <body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">
       <component type="typeof(App)" render-mode="ServerPrerendered" />
    </div>

    <script src="~/Adminlte/plugins/jquery/jquery.min.js"></script>
    <script src="~/Adminlte/plugins/jquery-ui/jquery-ui.min.js"></script>
    <script>$.widget.bridge('uibutton', $.ui.button)</script>
    <script src="~/Adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="~/Adminlte/plugins/chart.js/Chart.min.js"></script>
    <script src="~/Adminlte/plugins/sparklines/sparkline.js"></script>
    <script src="~/Adminlte/plugins/jqvmap/jquery.vmap.min.js"></script>
    <script src="~/Adminlte/plugins/jqvmap/maps/jquery.vmap.usa.js"></script>
    <script src="~/Adminlte/plugins/jquery-knob/jquery.knob.min.js"></script>
    <script src="~/Adminlte/plugins/moment/moment.min.js"></script>
    <script src="~/Adminlte/plugins/daterangepicker/daterangepicker.js"></script>
    <script src="~/Adminlte/plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
    <script src="~/Adminlte/plugins/summernote/summernote-bs4.min.js"></script>
    <script src="~/Adminlte/plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
    <script src="~/Adminlte/dist/js/adminlte.js"></script>
    <script src="~/Adminlte/dist/js/pages/dashboard.js"></script>
    <script src="~/Adminlte/dist/js/demo.js"></script>
 </body>
</html>

Below is Startup.cs
  app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
            endpoints.MapFallbackToPage("~/Admin/{*clientroutes:nonfile}", "/_HostAdmin");
        });

Project structure

3
  • Related: stackoverflow.com/q/65494101/60761 Commented May 2, 2021 at 7:35
  • @HenkHolterman : I changed my endpoint to endpoints.MapFallbackToPage("/Admin/{**segment}", "/Admin/_HostAdmin"); but it did not work Commented May 2, 2021 at 7:54
  • VS doesn't automatically run As Admin. If running in VS right click VS shortcut and select Run As Adim. The application will also run by just running the executable from outside VS. Commented May 2, 2021 at 9:49

1 Answer 1

3

You state

Now, the problem is that when I add new razor component and use AdminLayout.razor layout the view doesnot use css and js of _HostAdmin.cshtml.

and I assume your question is "Why".

You're adding a new razor component page to the existing application - probably with a route of something like "/admin/myadminpage". You're misunderstanding what's actually going on.

_Host.cshtml loads the SPA, but that's the only get/post that happens. Navigation after that is changing out components in the DOM. Loading a component with the layout AdminLayout just changes out the Layout component. There's no toing and froing with the server.

What you are trying to do requires a reload of the SPA. You could use:

NavigationManager.NavigateTo("/admin/myadminpage", true);

Also even if you do force a reload, the Fallbacks are in the wrong order - the default before the specific.

    endpoints.MapFallbackToPage("/_Host");
    endpoints.MapFallbackToPage("~/Admin/{*clientroutes:nonfile}", "/_HostAdmin");
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, it worked what is the correct order of fallback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.