Hello!

Today we will look at how you can change the error messages in Blazor Server. There are two types of errors page does not exist and exception. You can change the message that will be displayed in both cases. The first thing to consider is how to change the message displayed to the user in the case of exception.

Exception Error

In the Pages/_Host.cshtml file, this section is responsible for displaying the error:

    <div id="blazor-error-ui">
        <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>
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>

Depending on environment the required error is displayed. environment is determined by the environment variable ASPNETCORE_ENVIRONMENT if it is not specified then environment corresponds to Production. During development by default in Properties\launchSettings.json it is specified as Development.

{
    "BlazorLearn": {
    "commandName": "Project",
    "launchBrowser": true,
    "applicationUrl": "https://localhost:5001;http://localhost:5000",
    "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
}

Now I will change the message for the Development environment and get the following message. Error

Page not found error

The message that is displayed when the page is not found is in the file App.razor.

    <NotFound>
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>

There can be both a message and an entire html page. Replacing the text of the message I get: NotExist