The <base href='' >
is a client side technology that specifies the base URL for all relative URLs in current document. Many SPA frameworks , e.g. Angular, will use this element.
I have to set the @page to "/app/counter"
Actually, you don't have to and should never do that. One of the most important advantages when using <base>
is that it allows us to create an app without letting the components know about the base url.
Assuming you've changed the base href
from '/' to '/app/', and also changed the other relative urls:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorApp</title>
<base href="/app/" />
<link rel="stylesheet" href="/css/bootstrap/bootstrap.min.css" />
<link href="/css/site.css" rel="stylesheet" />
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="/_framework/blazor.server.js"></script>
</body>
</html>
Don't forget to prepend the default Blazor
Hub url with a /app/
:
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub("/app/_blazor");
endpoints.MapFallbackToPage("/_Host");
});
That's all. There's no need to change the routes from @page "/counter"
to @page "/app/counter"
at all.
Demo
Here's a demo that we don't change the @page
routes for components: