I would like to load a _Host.cshtml
file in an ASP.NET Core Blazor project (Server side Blazor) based on a header in the request.
For example:
A client connects to example.com
and is redirected to a _Host.cshtml
file specific for tenant A.
Another client connects to test.com
and is redirected to a _Host.cshtml
file specific for tenant B.
The _Host.cshtml
file looks somehow like this:
@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta charset="utf-8" />
<title>ProjectName</title>
<link rel="icon" type="image/png" sizes="32x32" href="images/tenantA/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="images/tenantA/favicons/favicon-16x16.png">
</head>
<body class="something">
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.Server))
</app>
<script src="_framework/blazor.server.js"></script>
<link href="css/tenantA/site.css" rel="stylesheet" />
</body>
</html>
In the _Host.cshtml
file, the reference to tenantA
needs to be set based on the above tenant selection from the tenant URL like described above. Is this possible and if yes how can this be achieved?
The easiest way is to use Route parameters instead of QueryString: @page "/navigatetopage/{id:int}/{key}" @code { [Parameter] public int Id{get;set;} [Parameter] public string Key{get;set;} ... }
By using Blazor route parameters, you can pass values from one page to another page. Use NavigationManager to pass a value and route the parameter to another page. Follow this example to achieve passing values from one page to another. Get the passed Page1 parameter value in Page2.
Razor components can be integrated into Razor Pages and MVC apps in a hosted Blazor WebAssembly solution.
This is one possible solution ,
In _Host.cshtml
You can redirect to any _HostX.cshtml by a dynamic string logic
@page "/"
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@if(Request.Query["test"].FirstOrDefault()=="1")
{
Html.RenderPartial("_Host2.cshtml",null,ViewData);
return;
}
<!DOCTYPE html>
<html lang="en">
<head>
....
...
..
.
This is tested.
You can modify the condition statement to match your case .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With