Blazor Relative url

3.9k views Asked by At

I have navlink defined as follows

<NavLink href="/intro"></NavLink>

In dev it works fine as the root of the site is at the same level as the blazor app. In prod I have to put the blazor site inside a folder in the default iis website. So my url becomes something like this

http://something/apps/cor/

So the <NavLink href="/intro"></NavLink> ends up trying to go to http://something/intro instead http://something/apps/cor/intro

I tried both /intro and intro and the both result in the wrong behavior. I am not getting how to make those links relative to blazor root not iis root.

3

There are 3 answers

1
americanslon On

Dot before the path. ./intro is how you do it

0
Bart Calixto On

on index.html you should have <base href="/apps/cor/" />

0
Pawel On

I had to do this trick in _Layout.cshtml:

@inject IWebHostEnvironment Environment;

// in <head> :
@if (Environment.IsDevelopment())
{
    <base href="~/" />
}
else
{
    <base href="~/myApp" />
}

then either <NavLink href="page"></NavLink> or <NavLink href="./page"></NavLink> works.

  • On localhost: https://localhost:44393/page
  • On server: https://example.com/myApp/page