3HMonkey
3HMonkey

Reputation: 137

404 Site not found when published a Blazor Web App as class library

My use case is relatively simple. I have a basic application that references various other applications, including, most recently, a Blazor web application. Unfortunately, I have the problem that as soon as I publish the application and start the EXE, I get a 404 error when accessing the page.

Steps to reproduce:

  1. Created a demo use case, what is an console application, published AOT and net8
  2. Created a Blazor Web Server application
  3. Switched the Blazor app to class library
  4. Referenced the Blazor app in the console app
  5. Edited the init class, so the console app can run the blazor app in another task
using System.Net;
using System.Reflection;
using MyBlazorApp.Components;

namespace MyBlazorApp
{
    public class MyBlazorAppCore
    {
        
        public void Initialize()
        {
            Task.Run(
                async () =>
                {
                    var builder = WebApplication.CreateBuilder();
                    var env = builder.Environment;
                    var root = "";
                    var webRoot = "";

                    if (env.IsDevelopment())
                    {
                        root = "C:\\MYPATHTOAPP\\";
                        webRoot = "C:\\MYPATHTOAPP\\wwwroot";
                    }
                    else
                    {
                        root = "\\";
                        webRoot = Path.Combine(Assembly.GetExecutingAssembly().Location, "wwwroot");
                    }

                    var options = new WebApplicationOptions()
                    {
                        ContentRootPath = root,
                        ApplicationName = Assembly.GetExecutingAssembly().GetName().Name,
                        WebRootPath = webRoot,
                    };
                   
                     
                    Console.WriteLine(builder.Environment.ContentRootPath);
                    Console.WriteLine(builder.Environment.WebRootPath);
                    Console.WriteLine(builder.Environment.ApplicationName);

                    builder.WebHost.ConfigureKestrel((context, serverOptions) =>
                    {
                        serverOptions.Listen(IPAddress.Loopback, 5000);
                        
                    });

                    // 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");
                        //app.UseHsts();
                    }

                    
                    //app.UseHttpsRedirection();

                    
                    app.UseStaticFiles();
                    app.UseAntiforgery();

                    app.MapRazorComponents<App>()
                       .AddInteractiveServerRenderMode();

                    await app.RunAsync();
                });

            
            
        }
    }
}

As you can see I had to set the paths since the blazor application is not running stand alone and referenced by another assembly.

  1. Therefore I was also in need of copying the content of the project's wwwroot folder to the output directory including the scopedcss/bundle/MyBlazorApp.styles.css .
<Target Name="CopyBundledCss" AfterTargets="Build;Publish">
  <ItemGroup>
    <BundledCssFiles Include="$(IntermediateOutputPath)scopedcss\bundle\**\*.css" />
  </ItemGroup>
  <Copy SourceFiles="@(BundledCssFiles)" DestinationFolder="$(OutDir)wwwroot" OverwriteReadOnlyFiles="true"/>
</Target>

<Target Name="CopyCssBundles" AfterTargets="AfterBuild">
  <ItemGroup>

    <MyCssBundles Include="$(IntermediateOutputPath)\scopedcss\bundle\$(AssemblyName).styles.css" />
  </ItemGroup>
  <Copy SourceFiles="@(MyCssBundles)" DestinationFiles="wwwroot\%(RecursiveDir)%(Filename)%(Extension)" OverwriteReadOnlyFiles="true" />
</Target>

  <ItemGroup>
  <Content Update="wwwroot\**\*">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

Running the application is working fine in Debug/Release in the Studio.

enter image description here enter image description here

But now to the problem. After publishing the application and running it, it also shows console output as expected.

enter image description here

But now when accessing the same site it shows 404, site not found. So the server responded with 404 in this case. This is confusing. Do you guys have any ideas?

enter image description here

Btw, the publish folder also contains the wwwroot.

THX for helping

Update: enter image description here enter image description here

Update2: I also noticed some other side effects when using it as class library / referenced by another app.

  1. The default counter component errors with 500 because of the rendermode
  2. "/Error" doesnt work:
30.05.2024 07:30:40 |   Debug    | 1 candidate(s) found for the request path '/Error'
30.05.2024 07:30:40 |   Debug    | Request matched endpoint '/Error (/Error)'
30.05.2024 07:30:40 |   Trace    | The endpoint does not specify the IRequestSizeLimitMetadata.
30.05.2024 07:30:40 |   Debug    | Static files was skipped as the request already matched an endpoint.
30.05.2024 07:30:40 |   Info     | Executing endpoint '/Error (/Error)'
30.05.2024 07:30:40 |   Debug    | Error handling in progress. Interactive components are not enabled.
30.05.2024 07:30:40 |   Debug    | Begin render root component 'App' with page 'Error'.
30.05.2024 07:30:40 |   Info     | Executed endpoint '/Error (/Error)'
30.05.2024 07:30:40 |   Error    | An exception was thrown attempting to execute the error handler.
30.05.2024 07:30:40 |   Error    | Connection id "0HN40F9JROBR7", Request id "0HN40F9JROBR7:00000002": An unhandled exception was thrown by the application.

Upvotes: 0

Views: 137

Answers (0)

Related Questions