3

In previous versions of Web Apps I could override the base path in production only (not development) by adding code to the _hosts.cshtml file. See Different base paths depending on Development/Production environment. However, in .NET 8 the Blazor Web App template no longer contains a _hosts.cshtml file.

How can I override the base path for production environments only in a .NET 8+ Blazor Web App (server-side)?

1 Answer 1

1

Check out the Migrate from ASP.NET Core 7.0 to 8.0: Convert a Blazor Server app into a Blazor Web App doc.

Especially 4th point:

Move the content in the _Host page (Pages/_Host.cshtml) to the empty App.razor file. Proceed to make the following changes to the App component.

...

Remove the following lines:

- <environment include="Staging,Production">
-     An error has occurred. This application may no longer respond until reloaded.
- </environment>
- <environment include="Development">
-     An unhandled exception has occurred. See browser dev tools for details.
- </environment>

Replace the preceding lines with the following:

@if (Env.IsDevelopment())
{
    <text>
        An unhandled exception has occurred. See browser dev tools for details.
    </text>
}
else
{
    <text>
        An error has occurred. This app may no longer respond until reloaded.
    </text>
}

...

So in your case it can look like:

@inject IHostEnvironment Env

@if (Env.IsDevelopment())
{
    <base href="/SomePath/"  />
}
else
{
    <base href="/" />
}
Sign up to request clarification or add additional context in comments.

1 Comment

That works - except for in my case I needed to switch the two base statements. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.