FranzHuber23
FranzHuber23

Reputation: 4322

ASP.Net Core Blazor: How to load different _Host.cshtml files based on header value

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?

Upvotes: 8

Views: 11243

Answers (1)

BlazorPlus
BlazorPlus

Reputation: 193

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 .

Upvotes: 10

Related Questions