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

不想你离开。 提交于 2020-05-13 19:24:08

问题


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?


回答1:


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 .



来源:https://stackoverflow.com/questions/60951806/asp-net-core-blazor-how-to-load-different-host-cshtml-files-based-on-header-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!