blazor
64 TopicsConvert the standard Blazor navigation menu to a collapsible icon menu
While I admittedly love Blazor I’ve always changed the out-of-the-box navigation menu that comes with it. It’s the first manoeuvre I pull when spinning up a new Blazor app, stripping out the purple gradient and getting it in, what I consider, a “blank slate state”. The other change I’ve wanted to make to the out-the-box look is one of those deluxe collapsible menus that leave just the icons showing. Anyone that’s used Azure DevOps will know what I’m talking about. I’ve included a picture to show DevOps example of what I’d like to see in my Blazor app. It gives a load of extra screen real estate which is always a priority for me in business applications particularly with complex or intensive workflows. Plus it gives the user the option to remove the text prompts once they are familiar with the system which is supported with carefully selected icon choices. As with most tasks that I assume will be an obvious solution I hit my search engine of choice and looked to avoid reinventing the wheel. However I found no source of pre-written changes to achieve this and was directed to fairly expensive third party controls to solve this one for me, which, being tight fisted, pushed me to do it for myself. Here I hope you save you the trouble of paying a pretty penny or having to wrestle the CSS into submission and provide a guide for producing a nice collapsible icon navigation menu by altering the existing out of the box menu in Blazor. In the following example I have left all the standard styling as is with the menu and just done the changes required to make the collapsible menu. The three files that require changes are MainLayout.razor, NavMenu.razor and NavMenu.razor.css. The code changes are shown below: Firstly the NavMenu.razor requires a bool value (IconMenuActive) to indicate whether the icon menu is showing or not, then wrap the text of the each NavItem in an if statement dependent on this bool. Then a method for toggling this bool and EventCalBack to send a bool to the MainLayout.razor for shrinking the width of the sidebar. Lastly there needs to be the control for switching menu views (I used the standard io icon arrows). NavMenu.razor <div class="top-row ps-3 navbar navbar-dark"> <div class="container-fluid"> <span class="oi oi-monitor" style="color:white;" aria-hidden="true"></span> @if (!@IconMenuActive) { <a class="navbar-brand" href="">The Menu Title Here</a> } <button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu"> <span class="navbar-toggler-icon"></span> </button> </div> </div> <div class="@NavMenuCssClass" @onclick="ToggleNavMenu"> <nav class="flex-column"> <div class="nav-item px-3"> <NavLink class="nav-link" href="" Match="NavLinkMatch.All"> <span class="oi oi-home" aria-hidden="true"></span> @if (!@IconMenuActive) { <label>Home</label> } </NavLink> </div> <div class="nav-item px-3"> <NavLink class="nav-link" href="counter"> <span class="oi oi-plus" aria-hidden="true"></span> @if (!@IconMenuActive) { <label>Counter</label> } </NavLink> </div> <div class="nav-item px-3"> <NavLink class="nav-link" href="fetchdata"> <span class="oi oi-list-rich" aria-hidden="true"></span> @if (!@IconMenuActive) { <label>Fetch data</label> } </NavLink> </div> </nav> </div> <div class="bottom-row"> <div class="icon-menu-arrow"> @if (!@IconMenuActive) { <span class="oi oi-arrow-left" style="color: white;" @onclick="ToggleIconMenu"></span> } else { <span class="oi oi-arrow-right" style="color: white;" @onclick="ToggleIconMenu"></span> } </div> </div> @code { //bool to send to MainLayout for shrinking sidebar and showing/hide menu text private bool IconMenuActive { get; set; } = false; //EventCallback for sending bool to MainLayout [Parameter] public EventCallback<bool> ShowIconMenu { get; set; } private bool collapseNavMenu = true; private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null; private void ToggleNavMenu() { collapseNavMenu = !collapseNavMenu; } //Method to toggle IconMenuActive bool and send bool via EventCallback private async Task ToggleIconMenu() { IconMenuActive = !IconMenuActive; await ShowIconMenu.InvokeAsync(IconMenuActive); } } Next I add in a bit of CSS in to NavMenu.razor.css to put the arrow for toggling the menu at the bottom of the sidebar and a media query to make sure it doesn't show up in mobile view. The CSS classes added are .bottom-row and .icon-menu-arrow. NavMenu.razor.css .navbar-toggler { background-color: rgba(255, 255, 255, 0.1); } .top-row { height: 3.5rem; background-color: rgba(0,0,0,0.4); } .bottom-row { position: absolute; bottom: 0; padding-bottom: 10px; text-align: right; width: 100%; padding-right: 28px; } .icon-menu-arrow { text-align: right; } .navbar-brand { font-size: 1.1rem; } .oi { width: 2rem; font-size: 1.1rem; vertical-align: text-top; top: -2px; } .nav-item { font-size: 0.9rem; padding-bottom: 0.5rem; } .nav-item:first-of-type { padding-top: 1rem; } .nav-item:last-of-type { padding-bottom: 1rem; } .nav-item ::deep a { color: #d7d7d7; border-radius: 4px; height: 3rem; display: flex; align-items: center; line-height: 3rem; } .nav-item ::deep a.active { background-color: rgba(255,255,255,0.25); color: white; } .nav-item ::deep a:hover { background-color: rgba(255,255,255,0.1); color: white; } @media (min-width: 641px) { .navbar-toggler { display: none; } .collapse { /* Never collapse the sidebar for wide screens */ display: block; } } @media (max-width: 640px) { .bottom-row { display: block; } } Finally I add in the handler for the EventCallback to MainLayout.razor and a method to alter the width of the sidebar. MainLayout.razor @inherits LayoutComponentBase <div class="page"> <div class="sidebar" style="@IconMenuCssClass"> <NavMenu ShowIconMenu="ToggleIconMenu"/> </div> <main> <div class="top-row px-4"> <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a> </div> <article class="content px-4"> @Body </article> </main> </div> @code{ private bool _iconMenuActive { get; set; } private string? IconMenuCssClass => _iconMenuActive ? "width: 80px;" : null; protected void ToggleIconMenu(bool iconMenuActive) { _iconMenuActive = iconMenuActive; } } The final product of these little changes are shown in the pictures below: I'd love to hear if anyone has tackled this in a different way to me and if they've got any ideas on making it cleaner. Have yourselves a wonderful day, Gav79KViews10likes19CommentsBlazor WASM PWA – Applications updates, cache busting with notification or force refresh
My team is currently working on a Blazor application and after several bouts of confusion between developers and testers we stuck a version number on the home screen and discovered the updates weren’t going out to everyone as we were expecting. We ended up with several people on different versions of the application and fortunately we spotted this issue early in development. I’ve put together the options and info I’ve found while trying to find a solution to this that works for our particular situation in hopes that it will help someone else dealing with this and hopefully open a discussion on how people are handling this issue and find any alternative solutions. To look at what was happening behind the scenes, I set up a simple WASM PWA on .NET7 and have it hosted on an Azure App Service. I stuck a version number on the home page so I can easily see when the update has taken place. The behaviour: Blazor WASM PWA applications load the application into the browser based on the cache pulled in by the service workers (Javascript assets that act as a proxy between browsers and web servers for offline support and performance). In dev tools you can see what’s in the cache storage for the app (go to dev tools > application tab > cache storage). The picture below shows that the cache contains “blazor-resources” and “offline-cache”. The “offline-cache” is the important one that determines what the site is displaying and on pushing an update the “blazor-resources” is unaffected so for this purpose can be ignored. Now the standard behaviour on pushing an update to the app is that the service worker will install the new app cache in the background and enter the “waiting” state and will not activate the newly cached app until the current application service worker is released, by either closing the browser or hard refreshing the app (Ctrl + F5). This behaviour is by design to prevent having two tabs open with different versions of the application running at once. However users may never close the application or browser or whatever and that would mean they could be running long retired versions of your application for an indeterminant length of time. Below you can see the cache storage after an update has been pushed stacking up the old and new version of the offline-cache. Once the service worker has been released then the old cache is removed the latest will be activated. This will then display the new version of the app. The solution: Now, what if we want to change this behaviour to either prompt and update or force one? Either way we will want to remove the need to close browsers or hard refresh as that can be a bit of a tricky one to get end users to get on board with. The service workers actually have a method that can skip the “waiting” stage of the service worker lifecycle clear the old cache and activate the latest. This is the SkipWaiting method. In the Blazor solution there is the service-worker.published.js file found in the wwwroot folder. This file contains the methods for the service worker. In this file there is a OnInstall method and can be modified to the following adding in the SkipWaiting method: async function onInstall(event) { console.info('Service worker: Install'); self.skipWaiting(); // Fetch and cache all matching items from the assets manifest const assetsRequests = self.assetsManifest.assets .filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url))) .filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url))) .map(asset => new Request(asset.url, { integrity: asset.hash, cache: 'no-cache' })); await caches.open(cacheName).then(cache => cache.addAll(assetsRequests)); } This additional line will now allow a standard refresh to update the application rather than requiring a hard refresh or closing the browser completely. When looking at the cache storage when an update is pushed you will see the second cache appear then the old cache disappear immediately. Note: Adding this behaviour will overwrite the intended behaviour and allow the potential of different version of the application running in different tabs. However we can look at avoiding that by forcing update later. Next, regardless of whether we want to prompt the user for an update or force it on them, we need a way of letting the site know it needs an update. My javascript is less than brilliant so I’ve used a solution by Wouter Huysentruit, full description here: https://whuysentruit.medium.com/blazor-wasm-pwa-adding-a-new-update-available-notification-d9f65c4ad13 This involves adding a js file called sw-registrator.js to the wwwroot folder that will allow an event to be triggered that can link to updating the site: window.updateAvailable = new Promise((resolve, reject) => { if (!('serviceWorker' in navigator)) { const errorMessage = `This browser doesn't support service workers`; console.error(errorMessage); reject(errorMessage); return; } navigator.serviceWorker.register('/service-worker.js') .then(registration => { console.info(`Service worker registration successful (scope: ${registration.scope})`); registration.onupdatefound = () => { const installingServiceWorker = registration.installing; installingServiceWorker.onstatechange = () => { if (installingServiceWorker.state === 'installed') { resolve(!!navigator.serviceWorker.controller); } } }; }) .catch(error => { console.error('Service worker registration failed with error:', error); reject(error); }); }); window.registerForUpdateAvailableNotification = (caller, methodName) => { window.updateAvailable.then(isUpdateAvailable => { if (isUpdateAvailable) { caller.invokeMethodAsync(methodName).then(); } }); }; Within index.html in the wwwroot folder we will need to register this new file instead of the service-worker.js. So we need to replace the reference to service-worker.js with the following: <script src="sw-registrator.js"></script> This will now raise an event once the cache refresh happens that can be used to trigger a prompt on the application or a refresh of the application. Now we need a razor component that will listen for the event and either throw up a refresh prompt or just force a refresh. Firstly I’ll do the prompt. For this make a new razor component and add the following code: @inject IJSRuntime _jsRuntime @inject NavigationManager uriHelper @if (_newVersionAvailable) { <button type="button" class="btn btn-danger shadow floating-update-button" onclick="window.location.reload()"> A new version of the application is available. Click here to upgrade. </button> } @code { private bool _newVersionAvailable = false; protected override async Task OnInitializedAsync() { await RegisterForUpdateAvailableNotification(); } private async Task RegisterForUpdateAvailableNotification() { await _jsRuntime.InvokeAsync<object>( identifier: "registerForUpdateAvailableNotification", DotNetObjectReference.Create(this), nameof(OnUpdateAvailable)); } [JSInvokable(nameof(OnUpdateAvailable))] public Task OnUpdateAvailable() { _newVersionAvailable = true; StateHasChanged(); return Task.CompletedTask; } } I personally like a red bar across the top as it’s very attention grabbing so you can add the following css file called WhateverYouCalledTheComponent.razor.css: .floating-update-button { position: relative; top: 0; padding: 1rem 1.5rem; width: 100%; } This component can then be placed at the top of MainLayout.razor above the body to ensure it appears anywhere on the app the user happens to be. This will then produce the following result once an update is pushed out: Once the banner is clicked, it will reload the site and the new update will be running. This is the setup that I will be implementing on my teams project as I wouldn’t want the site to force updates in case a user was in the middle of data entry. However if a forced refresh is desired, it’s a quick change to the razor component to allow that. Change the components code to the following: @inject IJSRuntime _jsRuntime @inject NavigationManager uriHelper @code { protected override async Task OnInitializedAsync() { await RegisterForUpdateAvailableNotification(); } private async Task RegisterForUpdateAvailableNotification() { await _jsRuntime.InvokeAsync<object>( identifier: "registerForUpdateAvailableNotification", DotNetObjectReference.Create(this), nameof(OnUpdateAvailable)); } [JSInvokable(nameof(OnUpdateAvailable))] public Task OnUpdateAvailable() { uriHelper.NavigateTo(uriHelper.Uri, forceLoad: true); StateHasChanged(); return Task.CompletedTask; } } Now on pushing an update you will see that the update is installed in the cache and once the event is triggered the old cache is removed and the site reloads wherever it happens to be. I’d be really interested to hear other peoples takes on this or if they’ve found other methods of achieving this, I’ve scoured the web and this was the combination of the best methods I came across but certainly open to improvement.15KViews0likes5CommentsCould not set or bind model property with Bootstrap Datepicker in Blazor
I am using bootstrap datepicker and the problem is that when I pick a date, it does not fire a change or input event and noting is binding with the model property Course.StartDate or Course.EndDate. The default datepicker works but does not support Afghanistan datetime. That is why I use boostrap datepicker. Blazor code: @using Microsoft.AspNetCore.Mvc.Rendering @using myproject.Data @using Microsoft.JSInterop; @inject myproject.Repository.CoursesRepository _coursesRepository @inject IJSRuntime JS <EditForm Model="@Course" OnValidSubmit="e=> { if(selectedId == 0) { addCourse(); } else { updateCourse(Course.CourseId); } }"> <div class="mb-2"> <div>@Course.StartDate</div> <label class="col-form-label" for="StartDate">@Loc["Start Date"]<span class="text-danger fs--1">*</span>:</label> <InputDate class="form-control" @bind-Value="Course.StartDate" @bind-Value:format="yyyy-MM-dd" id="StartDate" /> <ValidationMessage class="text-danger" For="(() => Course.StartDate)"/> </div> <div class="mb-2"> <label class="col-form-label" for="EndDate">@Loc["End Date"]<span class="text-danger fs--1">*</span>:</label> <InputDate class="form-control" @bind-Value="Course.EndDate" @bind-Value:format="yyyy-MM-dd" id="EndDate"/> <ValidationMessage class="text-danger" For="(() => Course.EndDate)"/> </div> </EditForm> @code { public CourseModel Course = new(); public string[] dates = new string[] { "#StartDate", "#EndDate" }; protected override void OnAfterRender(bool firstRender) { base.OnAfterRender(firstRender); loadScripts(); } void addCourse() { _coursesRepository.AddCourse(Course); FillData(); Course = new(); var title = "Course"; Swal.Success(title : Loc[$"{title} added successfully"],toast : true); } // initializes the datepicker public async Task loadScripts() { await JS.InvokeVoidAsync("initializeDatepicker", (object) dates); } } This is script for initializing the datepickers <script> function initializeDatepicker(dates) { dates.forEach((element) => { $(element).datepicker({ onSelect: function(dateText) { // this is not working element.value = this.value; /* tried this and still not working $(element).trigger("change"); also tried this and still not working $(element).change(); */ // this is working console.log("Selected date: " + dateText + "; input's current value: " + this.value); }, dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true }); }); } </script>Solved5KViews0likes2CommentsUsing browser local storage with Blazor
Project based on .NET Blazor Web App in Server render mode. Blazor currently has no built-in function for utilizing local browser storage. However this feature can come in useful for storing user information or partially completed forms etc. Luckily implementing a class for facilitating this function is straightforward enough. Here I’ll run over the steps required to get local storage up and running in your Blazor web app. Note: This method uses JSRuntime so make sure if you want to call it on page/component load you will need to run it on “OnAfterRender” rather than “OnInitialize” otherwise you will get an error. Here is a screenshot of my file structure so you can see the additional files I’ve made and where I’ve placed them: Making the javascript functions: Within the wwwroot of your app, make a new javascript file (I’ve called mine LocalStorage.js) and enter the following code: window.LocalStorageActions = { setItem: function (key, value) { localStorage.setItem(key, value); }, getItem: function (key) { return localStorage.getItem(key); }, removeItem: function (key) { localStorage.removeItem(key); }, clearData: function () { localStorage.clear(); } } These are the four functions we will need for setting, getting, removing and clearing local storage in the browser. Make a C# class to call the functions: To keep things tidy, I’ve put the C# class for calling the local storage functions and it’s interface into a folder called helpers, but you can put this wherever it’s most appropriate in your solution. You can create a new .cs file and add the following code: using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; namespace BlazorLocalStorage.Helpers { public class LocalStorageHelper : ILocalStorageHelper { private readonly IJSRuntime _javaScript; public LocalStorageHelper(IJSRuntime javaScript) { _javaScript = javaScript; } public async Task SetItem(string key, string value) { await _javaScript.InvokeAsync<string>("LocalStorageActions.setItem", key, value); } public async Task<string> GetItem(string key) { return await _javaScript.InvokeAsync<string>("LocalStorageActions.getItem", key); } public async Task RemoveItem(string key) { await _javaScript.InvokeAsync<string>("LocalStorageActions.removeItem", key); } public async Task ClearData() { await _javaScript.InvokeAsync<string>("LocalStorageActions.clearData"); } } } To quickly produce the interface for this class you can right click the class name (LocalStorageHelper in this case) and select “Quick Actions and Refactoring” and then select “Extract Interface” This will open a popup for extracting an interface from the class that you can select OK and a new file called ILocalStorageHelper.cs will appear and the LocalStorageHelper class will now inherit from the interface that’s been created. Registering the new files ready for use: Firstly we want to reference the new javascript file for use in the application. To do this go to the App.razor file and add the following line in the <body> section of the file. <script src="LocalStorage.js"></script> Next, we need to register the LocalStorageHelper so it can be injected wherever we want to use it. To do this open the Program.cs file and add the following line before the builder.Build() method it called. builder.Services.AddScoped<ILocalStorageHelper, LocalStorageHelper>(); Let’s try it out: I’ve made a little example of the new class being used by modifying the Home.razor file to use the various functions we’ve created. Before adding the main code I have created a little css class to centre the content of the page to keep things neat. So you can add the following code snippet to app.css. .center-screen { display: flex; justify-content: center; align-items: center; text-align: center; min-height: 70vh; } Now we can go into the Home.razor file and replace the content with the following. PAGE "/" @using BlazorLocalStorage.Helpers @rendermode InteractiveServer <PageTitle>Home</PageTitle> <div class="center-screen"> <div> <div class="row"> <label>Text-1</label> <InputText class="m-2" @bind-Value="Text1" /> </div> <div class="row"> <button class="btn btn-success m-2" @onclick="Save">Save</button> <button class="btn btn-info m-2" @onclick="Load">Load</button> <button class="btn btn-warning m-2" @onclick="Remove">Remove</button> <button class="btn btn-danger m-2" @onclick="Clear">Clear</button> </div> </div> </div> @code { [Inject] ILocalStorageHelper LocalStorage { get; set; } public string Text1 { get; set; } public async Task Save() { await LocalStorage.SetItem("textData", Text1); } public async Task Load() { Text1 = await LocalStorage.GetItem("textData"); } public async Task Remove() { await LocalStorage.RemoveItem("textData"); } public async Task Clear() { await LocalStorage.ClearData(); } } Apologies for the garish use of coloured buttons but it’s just meant to be an obvious demo. If you open the browsers developer tools and navigate to “Application” the select “Local Storage” you will be able to see the data stored, updated or removed as you use the demo. If you refresh the page or close and open the browser and select load it will pull the stored data back in to the textbox. That’s all there is to it! If you want to see the project, I've stuck a copy up on github that you can have a look at: https://github.com/DrGav/BlazorLocalStorage Have yourselves a wonderful day, Gavin.2.3KViews0likes0CommentsServer-side Blazor state management
One of the more interesting things about server-side Blazor is that, unlike all previous UI frameworks, it maintains all per-user state (including the user's identity) in a dependency injection scope. Windows Forms, WPF, and UWP use the current thread. ASP.NET normally uses HttpContext. Blazor WebAssembly is similar to WinForms/WPF - a single process space for the user and app. But server-side Blazor is stateful on the server, so it can't rely on HttpContext, or any specific thread. So the way they implemented management of things like the current user's principal/identity is via DI scope. Each server-side Blazor app is hosted in its own DI scope, and so a service added as a scoped service is exactly what is necessary to maintain any per-user information. The downside to this, is that if you (like me) have any static methods that, for example, check the user's permissions based on user identity - well, that's impossible. It is impossible because the user's identity isn't "ambient" like in all other UI frameworks. The user's identity is only available in an _instance_ of a type, and only if a service that has the user identity was injected into your instance! As a result, I've spent many, many hours over the past couple weeks rewriting the #cslanet framework so it gets the user identity via DI, and then passes the identity around to all objects that need it (which is a lot - CSLA is primarily a rules engine, so the user identity is necessary all over the place). It turns out that DI, like async/await, is like a virus. Once you start using it a little, pretty soon you find that you've had to rewrite nearly all your code to rely on the concept.2.2KViews3likes1CommentSimple forms-based web app authenticated against Azure AD - Need advice re framework / tools to use
We have a need for a simple forms-based application that fronts SQL Server data, but this one needs to be secure. And we also need to support named users from outside the firm. It seems like a .NET web application is what we're looking for, with Azure AD as our source of authentication. But as a novice .NET programmer, I find it confusing trying to sort through all the advice and tutorials and best practices out there, many of which are not dated. Much of the advice seems out of date or inappropriate with today's technology. We're planning to use C# and .NET 6 for this project and have Visual Studio Enterprise for development. We'd like to get off on the right foot and use current technology and techniques. For the application, we're looking for easy rapid development of a forms and event driven application, similar to what we're used to when using Access forms, buttons, controls for rapid development of utility programs. Is there a recommended framework, or tool, or project type to support this kind of coding? While I can do HTML and CSS and client-side Java if I have to, I'd prefer not to be involved in all the intricacies of the form presentation. Visual forms design is a definite plus. For security, we're looking to authenticate the user against Azure AD, with MFA. We've got experience setting up other people's applications to authenticate against Azure AD, but never had to write one ourselves. Is OWIN the current preferred method and, if so, is there a preferred library to use or tutorial to follow that is relevant to today's technology and .NET 6? For reference, in the past, we'd often make Access front ends for quick and easy tools, especially those that interacted with SQL server databases. The visual forms and easy event driven programming made it quite cost effective to develop and support these important but low-complexity projects. We'd build a few forms, each fronting a table or view, add a few buttons or other controls with a couple of key lines of code to implement our logic and rules, and be all set. The fact that users could, if they tried, bypass the code and see the linked tables was not a concern in these cases. Now it is. We'd love that kind of ease of use, with the addition of webhosting and AAD authentication. I've done a good deal of development over 25+ years in many languages and worked with client server tools, SQL, remote procedure calls, and REST APIs. I'm currently working mostly in Python and some VB for Microsoft programs, though I can get by in C-like languages when needed. Thanks in advance for any advice on what the current .NET Web equivalent of our old tools would be. I don't mind putting in the effort to learn but want to be heading in the right direction.1.8KViews0likes1CommentBest practice for inject Dependency service in Blazor
I am in process to start develop an ERP Application in Blazor server. I am new in Blazor technology. I want suggestion in DI. I have a class called Setting. This class is used for store application’s setting at the time of user login and consume it in at every component level. And this class have reference type multiple properties. I want suggestion that what is best practice to handle these properties. There is multiple ways to handle these property. please suggest me which is best way or any new way is also welcome. Base Class public class CompanyBase { public int ID { get; set; } public string? Name { get; set; } } public class OptionBase { public int Option1 { get; set; } public string? Option2 { get; set; } } Way No 1 public class Setting { public CompanyBase User { get; } = new (); public OptionBase Option { get; } = new(); } Program.cs builder.Services.AddTransient<Setting>(); FetchData.razor @inject Setting setting Way No 2 public class Setting { public CompanyBase User { get; init; } public OptionBase Option { get; init; } public Setting(CompanyBase user, OptionBase option) { User = user; Option = option; } } Program.cs builder.Services.AddTransient<CompanyBase>(); builder.Services.AddTransient<OptionBase>(); builder.Services.AddTransient<Setting>(); FetchData.razor @inject Setting setting Way No 3 No Setting Class Program.cs builder.Services.AddTransient<CompanyBase>(); builder.Services.AddTransient<OptionBase>(); FetchData.razor @inject CompanyBase Company @inject CompanyBase Option1.8KViews0likes1Comment