Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 701
d00d Avatar asked Oct 21 '25 06:10

d00d


2 Answers

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>
like image 200
d00d Avatar answered Oct 23 '25 20:10

d00d


For .net-8.0 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="/" />
}
like image 32
Guru Stron Avatar answered Oct 23 '25 22:10

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!