I was very excited to see preview 3 of .NET 6 come out, especially with the hot reload enabled for Blazor.
Not wanting to tamper with my main Windows environment, I installed dotnet 6 into my WSL2 Ubuntu instance.
And immediately discovered that I was unable to access my Blazor server endpoint from Windows via localhost. This should be automatic and require no effort!
I tweeted my issue and got lots of feedback on how you need to do a bunch of manual steps, including editing files in the project and more. But again, this should be automatic and should “just work”.
So I tried it on a different physical computer, and it worked great! Ah ha! My problem is narrowed to my workstation.
I wasn’t entirely surprised, as I’ve tweaked my WSL2 on my workstation to enable systemd (which isn’t supported by WSL2). So I installed a new WSL2 Ubuntu instance, but it failed also.
Fortunately @craigloewen provided a tip about “fast startup”, which I thought might be a WSL2 feature, but actually turns out to be a Windows 10 feature.
Thankfully there’s already a blog post with instructions on how to disable Windows 10 Fast Startup to resolve the WSL2 localhost redirect failure. Why this doesn’t appear anywhere near the top of searches about being unable to access WSL 2 websites from localhost I don’t know, because it did fix the issue.
In summary, if you create a web site, web app, or Blazor app in WSL2 and do
dotnet run, you should be able to open a browser in the Windows host and navigate to localhost. It should just work.
If it doesn’t work, apparently this fast start issue is the first thing to try. And if turning off fast start doesn’t solve the problem, then you can try all the workarounds like changing the targets to 0.0.0.0, editing host files, and all the other irrelevant workarounds I tried.