d00d
d00d

Reputation: 763

Different base paths depending on Development/Production environment

I just published a Blazor app on our development server to test if everything is running fine. I set up the server so that i would have to access the app under an address like that:

serveraddress:8090/UserManagement

On my local development machine I don't need the UserManagement folder, I just access and redirect directly to my pages. My index page for example would be available at

@page "/"

on my local machine. But when I run the software on the development server, there should be an additional /UserManagement tag added to the path. I set the ASPNETCORE_ENVIRONMENT variable to "Development" on my server and tried to change the tag in the _Hosts.cshtml as follows:

<environment include="Development">
    <base href="~/UserManagement" />
</environment>
<environment include="Production">
    <base href="~/" />
 </environment>

Unfortunately, this does not work and all the redirects go to / instead of /UserManagement/. Any idea?

Upvotes: 3

Views: 3158

Answers (2)

Guru Stron
Guru Stron

Reputation: 142873

For the environments part was migrated to App.razor file (see Migrate from ASP.NET Core 7.0 to 8.0: Convert a Blazor Server app into a Blazor Web App) and can look like the following:

@inject IHostEnvironment Env

@if (Env.IsDevelopment())
{
    <base href="/UserManagement/"  />
}
else
{
    <base href="/" />
}

Upvotes: 3

d00d
d00d

Reputation: 763

Okay I think i found the solution. First I changed the base url so that only when the Development environment variable is found, the base url /UserManagement will be used:

_Hosts.cshtml:

<environment include="Development">
        <base href="/UserManagement/" />
    </environment>
    <environment exclude="Development">
        <base href="~/" />
    </environment>

Then i added this to the very beginning of the startup.cs' configure-method:

app.UsePathBase("/UserManagement");

But that alone still did not do the trick. The problem was, that I used hrefs links like that to forward to another page:

<a href="/CreateD5User" class="btn btn-success">New User</a>

With the leading slash, the UserManagement directory was not preprended. However, after removing the leading slash like this it worked and the /UserManagement/ was added.

<a href="CreateD5User" class="btn btn-success">New User</a>

Upvotes: 6

Related Questions