Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Adding Razor Pages from another project into Primary Blazor Web App Project

$
0
0

I've moved the scaffolded Dashboard Razor Page from a blazor server app into a separate project(RCL). There are no build/compile or runtime errors. The project runs. I am trying to develop a modular blazor application where each RCL will work as it's own UI entity and I will be able to access it through navigation menu.

Folder structure below,

Folder PATH listing for volume DATAVolume serial number is 2224-6101E:.?   WebPortal.slnx?   +---WebPortal?   ?   appsettings.Development.json?   ?   appsettings.json?   ?   Program.cs?   ?   WeatherForecast.cs?   ?   WebPortal.API.csproj?   ?   WebPortal.API.csproj.user?   ?   WebPortal.http?   ?               ?   +---Controllers?   ?       WeatherForecastController.cs?   ?       ?   +---obj?   ?   +---Debug?   ?       +---net10.0?   +---Properties?           launchSettings.json?           +---WebPortal.Core?   ?   IModule.cs?   ?   NavItem.cs?   ?   WebPortal.Core.csproj?                       +---WebPortal.Module.Dashboard?   ?   DashboardModule.cs?   ?   ExampleJsInterop.cs?   ?   WebPortal.Module.Dashboard.csproj?   ?   _Imports.razor        ?   +---Pages?   ?       Dashboard.razor?   ?       Dashboard.razor.css?   ?       ?   +---wwwroot?           background.png?           exampleJsInterop.js?           +---WebPortal.UI    ?   appsettings.Development.json    ?   appsettings.json    ?   Program.cs    ?   WebPortal.UI.csproj    ?   WebPortal.UI.csproj.user    ?   +---bin    ?   +---Debug    ?       +---net10.0    ?               appsettings.Development.json    ?               appsettings.json    ?               WebPortal.Core.dll    ?               WebPortal.Core.pdb    ?               WebPortal.Module.Dashboard.dll    ?               WebPortal.Module.Dashboard.pdb    ?               WebPortal.Module.Dashboard.staticwebassets.endpoints.json    ?               WebPortal.Module.Dashboard.staticwebassets.runtime.json    ?               WebPortal.UI.deps.json    ?               WebPortal.UI.dll    ?               WebPortal.UI.exe    ?               WebPortal.UI.pdb    ?               WebPortal.UI.runtimeconfig.json    ?               WebPortal.UI.staticwebassets.endpoints.json    ?               WebPortal.UI.staticwebassets.runtime.json    ?               +---Components    ?   ?   App.razor    ?   ?   Routes.razor    ?   ?   _Imports.razor    ?   ?       ?   +---Layout    ?   ?       MainLayout.razor    ?   ?       MainLayout.razor.css    ?   ?       NavMenu.razor    ?   ?       NavMenu.razor.css    ?   ?       ReconnectModal.razor    ?   ?       ReconnectModal.razor.css    ?   ?       ReconnectModal.razor.js    ?   ?           ?   +---Pages    ?           Counter.razor    ?           Error.razor    ?           Home.razor    ?           NotFound.razor    ?           Weather.razor    ?           +---Helpers    ?       AssemblyScanning.cs    ?   +---Debug    ?       +---net10.0    ?           +---ref    ?           ?       WebPortal.UI.dll    ?           ?           ?           +---refint    ?           ?       WebPortal.UI.dll    ?           ?           ?           +---scopedcss    ?           ?   +---bundle    ?           ?   ?       WebPortal.UI.styles.css    ?           ?   ?           ?           ?   +---Components    ?           ?   ?   +---Layout    ?           ?   ?           MainLayout.razor.rz.scp.css    ?           ?   ?           NavMenu.razor.rz.scp.css    ?           ?   ?           ReconnectModal.razor.rz.scp.css    ?           ?   ?               ?           ?   +---projectbundle    ?           ?           WebPortal.UI.bundle.scp.css    ?           ?               ?           +---staticwebassets+---Properties    ?       launchSettings.json    ?       +---wwwroot        ?   app.css        ?   favicon.png        ?   +---lib+---bootstrap+---dist+---css                     +---js                                                       

Whe I browse to https://localhost:7260/dashboard, it says "Sorry, the content you are looking for does not exist."

Please find my Routes.razor and Program.cs file below.

Routes.razor

@using WebPortal.UI.Helpers<Router AppAssembly="@typeof(Program).Assembly" NotFoundPage="@typeof(Pages.NotFound)"        AdditionalAssemblies="@AssemblyScanning.GetAssemblies().ToArray()"><Found Context="routeData"><RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" /><FocusOnNavigate RouteData="@routeData" Selector="h1" /></Found></Router>

Program.cs

using WebPortal.UI.Components;var builder = WebApplication.CreateBuilder( args );// Add services to the container.builder.Services.AddRazorComponents()    .AddInteractiveServerComponents();var app = builder.Build();// Configure the HTTP request pipeline.if( !app.Environment.IsDevelopment() ){  app.UseExceptionHandler( "/Error", createScopeForErrors: true );  // 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.UseStatusCodePagesWithReExecute( "/not-found", createScopeForStatusCodePages: true );app.UseHttpsRedirection();app.UseRouting();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>()    .AddInteractiveServerRenderMode();app.Run();

AssemblyScanning.cs

public static class AssemblyScanning{  // Thread-safe, lazy, cached result  private static readonly Lazy<List<Assembly>> _cachedAssemblies =      new Lazy<List<Assembly>>( LoadAssemblies, isThreadSafe: true );  public static List<Assembly> GetAssemblies()  {    return _cachedAssemblies.Value;  }  private static List<Assembly> LoadAssemblies()  {    List<Assembly> allAssemblies = new();    string path = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location )!;    var files = Directory.GetFiles( path, "*.dll" );    foreach( string dll in files.Where( x =>                 Path.GetFileName( x ).StartsWith( "WebPortal.Module" ) ) )    {      // IMPORTANT: Use LoadFrom, not LoadFile      allAssemblies.Add( Assembly.LoadFrom( dll ) );    }    return allAssemblies        .Where( a =>            a.GetTypes().Any( t =>                t.GetInterfaces().Contains( typeof( IModule ) ) ) )        .ToList();  }  public static IEnumerable<TypeInfo> GetTypes<T>()  {    return GetAssemblies()        .SelectMany( a =>            a.DefinedTypes.Where( x =>                x.GetInterfaces().Contains( typeof( T ) ) ) );  }  public static IEnumerable<T> GetInstances<T>()  {    foreach( Type implementation in GetTypes<T>() )    {      if( !implementation.IsAbstract )      {        yield return (T)Activator.CreateInstance( implementation )!;      }    }  }}

I'm a little stuck on where to go from here in order to get the razor pages working from my blazor server app.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>