crmcode2012
crmcode2012

Reputation: 13

Blazor page rendering twice

UPDATE 2: Adding Mainlayout.razor and NavMenu.razor

MainLayout.razor


@inherits LayoutComponentBase

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <div class="main">
        <div class="top-row px-4">
            <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
        </div>

        <div class="content px-4">
            @Body
        </div>
    </div>
</div>

NavMenu.razor


<div class="top-row pl-4 navbar navbar-dark">
    <a class="navbar-brand" href="">HR Test App</a>
    <button class="navbar-toggler" @onclick="ToggleNavMenu">
        <span class="navbar-toggler-icon"></span>
    </button>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
    <ul class="nav flex-column">
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                <span class="oi oi-home" aria-hidden="true"></span> Home
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="HR\employeedisplay\data">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Employee Data
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="HR\employeedisplay\hierarchy">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Employee Hierarchy
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="HR\departmentlocation">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Department/Location
            </NavLink>
        </li>
        <li class="nav-item px-3">
            <NavLink class="nav-link" href="HR\departments">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Department Data
            </NavLink>
        </li>
    </ul>
</div>

@code {
    private bool collapseNavMenu = true;

    private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        collapseNavMenu = !collapseNavMenu;
    }
}

UPDATE: Adding my startup.cs and program.cs files and screenshots

Shot #1: This is the page as shown after beginning the debugging Initial page after starting debug

Shot #2: If I scroll the page you will see the that everything is duplicated. Same page when I start scrolling

Startup.cs


using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BO;
using Blazored.Modal;

namespace BlazorHRTestApp
{
    public class Startup
    {
        public static string HRDataConn { get; set; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddBlazoredModal();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            //Set the connection strings
            AppSettings.ConnectionString = Configuration.GetConnectionString("HRData");

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

Program.cs


using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorHRTestApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

So I noticed that my Blazor app is rendering each page twice and I am lost as to why. I checked some other posts and changed my render-mode to "Server" as seen below in my _Hosts.js file:


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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>BlazorHRTestApp</title>
    <base href="~/" />
    <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
    <link href="css/site.css" rel="stylesheet" />
    <link href="BlazorHRTestApp.styles.css" rel="stylesheet" />
    <link href="_content/Blazored.Typeahead/blazored-typeahead.css" rel="stylesheet" />
    <link href="_content/Blazored.Modal/blazored-modal.css" rel="stylesheet" />
</head>
<body>
    <component type="typeof(App)" render-mode="Server" />

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>

    <script src="_framework/blazor.server.js"></script>
    <script src="_content/Blazored.Typeahead/blazored-typeahead.js"></script>
    <script src="_content/Blazored.Modal/blazored.modal.js"></script>
</body>
</html>

And here is my Index.razor file as an example of a file that is being rendered twice:


@page "/"

<h1>Human Resources</h1>

<span>This is a Blazor test to check to see how well Blazor works.</span>
<br />

@code {

}

I have looked into the lifecycle, but can't seem to see anything in my code. And this is happening during debug on my laptop.

Upvotes: 1

Views: 676

Answers (1)

crmcode2012
crmcode2012

Reputation: 13

So I decided to run a test and created a blank Blazor project and added my files in and this issue went away. I have had several issues with the laptop so it is possible something happened in a crash that caused this issue.

If this presents itself again, I will come back but add more files from the start.

Thanks for any and all help.

Upvotes: 0

Related Questions