Css "not found"

Naw. Nothing quite that simple. Although I’m sure the solution will be one or two settings…

See near the end of How to deal with the “HTTP 404 ‘_content/Foo/Bar.css’ not found” when using Razor component package on ASP.NET Core Blazor app - DEV Community maybe its set differently when building for mac on windows?

Thanks, but I had tried that as well.

Interestingly, I just received VS2022 for MacOS, and creating the Blazor Server app on there, when run in the IDE, works fine, but when built, has the same issue as when building from VS2022 on Windows for MacOS.

At this point, I’d say the 2 IDEs build the same output, except of course that the MacOS IDE can only build for MacOS, while the Windows IDE can build for all platforms…

I’ve hit every forum I can find, and still no answers. As I mentioned, most .NET forums only contain Windows users. :frowning:

I suspect .Net on the Mac is still a small mindshare thing
And so few people doing it with any degree of seriousness

1 Like

I finally found a genius .NET programmer through my brother. He figured it out in a few minutes. It’s so simple, but had eluded me for ages.

Add 1 line in program.cs:

webBuilder.UseContentRoot(AppContext.BaseDirectory);

Old program.cs:

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

namespace MacOSBlazorApp1
{
    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>();
                });
    }
}

After:

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

namespace MacOSBlazorApp1
{
    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>();
                    webBuilder.UseContentRoot(AppContext.BaseDirectory);
                });
    }
}

I have no idea why this has to be added on MacOS but not Windows. Somehow the defaults are different on MacOS I guess!

It might be related to this: macOS Catalina Notarization and the impact on .NET Core downloads and projects

Naw…

AppHost is disabled by default on MacOS, which could mean that UseContentRoot must be set explicitly.

I’m building self-contained.

“An appHost is always created when you publish your app self-contained.”

So the answer, if anyone runs into this, is to add just a single line in Program.cs:
webBuilder.UseContentRoot(AppContext.BaseDirectory);
after the line
webBuilder.UseStartup<Startup>();

Whole Program.cs after modification:

using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace MacOSBlazorApp1
{
    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>();
                    webBuilder.UseContentRoot(AppContext.BaseDirectory);
                });
    }
}

In .NET 6, Program.cs looks quite different.

public class Program
{

	static void Main(string[] args)
	{
		var builder = WebApplication.CreateBuilder(new WebApplicationOptions
		{
	        ContentRootPath = AppContext.BaseDirectory,
            Args = args
		});

		// Add services to the container.
		builder.Services.AddRazorPages();
		builder.Services.AddServerSideBlazor();
		
		var app = builder.Build();

		// Configure the HTTP request pipeline.
		if (!app.Environment.IsDevelopment())
		{
			app.UseExceptionHandler("/Error");
		}

		app.Urls.Add("http://*:5001"); // Allows access from anywhere on LAN
		app.UseStaticFiles();
		app.UseRouting();
		app.MapBlazorHub();
		app.MapFallbackToPage("/_Host");

		app.Run();

	}
}

Without adding the lines:

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    ContentRootPath = AppContext.BaseDirectory,
    Args = args
});

It will have the same issue as in the OP.

The default line in Program.cs is:

var builder = WebApplication.CreateBuilder(args);
1 Like