Search code examples
asp.net-corerazorblazor

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?


Solution

  • 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>