Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 376
FranzHuber23 Avatar asked Mar 31 '20 13:03

FranzHuber23


People also ask

How do you pass multiple parameters in Blazor?

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;} ... }

How do you pass data between pages in Blazor?

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.

Can you mix Blazor and Razor?

Razor components can be integrated into Razor Pages and MVC apps in a hosted Blazor WebAssembly solution.


1 Answers

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 .

like image 80
BlazorPlus Avatar answered Sep 25 '22 06:09

BlazorPlus