本文介绍了多租户 Razor 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 Razor Pages 路由以允许为不同的租户呈现不同的视图.

我的目录结构如下:

/Pages测试.cshtml.cs/租户1测试.cshtml/租户2测试.cshtml

鉴于我已经能够决定需要哪个租户,如何配置路由以将某些路径(例如 localhost:8080/Test)映射到 Tenant1/TestTenant2/Test 视图.

解决方案

使用动态视图内容(通过部分视图).

使用此解决方案,Test 页面将根据用于调用它的路由动态加载不同的视图.

这意味着您只有一个 Test 页面,但在 cshtml 文件中,您将从部分视图中获取内容(稍后会详细介绍).

首先你需要像这样重命名文件......

/Pages测试.cshtml.cs/租户1_Test.cshtml//注意它以下划线为前缀!/租户2_Test.cshtml//也以下划线为前缀.

局部视图的命名约定是在文件前面加上下划线 (_).这将立即被查看您的项目文件的人识别为不可路由的"文件.页面.

然后你添加一点逻辑来渲染局部视图......

Test.cshtml

@{switch(...)//使用switch语句来说明解决方案{案例租户 1":await Html.PartialAsync("~/Pages/Tenant1/_Test.cshtml");休息;案例租户 2":await Html.PartialAsync("~/Pages/Tenant2/_Test.cshtml");休息;默认:抛出新的 NotImplementedException();}}

您可以阅读部分视图 这里.

额外:使用相同的页面模型.
我还注意到您曾想使用相同的页面模型(意味着为两者共享 Test.cshtml.cs.这相当简单,但为了回答的完整性,这里是您将如何这样做...

/Pages/Test.cshtml.cs

命名空间 Foo.Pages{公共类 MySharedTestModel : PageModel{...}}

/Pages/Tenant1/Test.cshtml/Pages/Tenant2/Test.cshtml

@page@using Foo.Pages@model MySharedTestModel...

I am trying to set up Razor Pages routing to allow different views to be rendered for different tenants.

I have a directory structure as follows:

/Pages
    Test.cshtml.cs
    /Tenant1
        Test.cshtml
    /Tenant2
        Test.cshtml

Given I am already able to decide which tenant is required, how is it possible to configure the routing to map some path eg localhost:8080/Test to either Tenant1/Test or Tenant2/Test views.

解决方案

Use dynamic view content (via partial views).

With this solution, the Test page will dynamically load a different view depending on the route used to call it.

This means that you only have a single Test page but inside the cshtml file you will grab content from a partial view (more on that in a second).

First you will need to rename the files like so....

/Pages
    Test.cshtml.cs
    /Tenant1
        _Test.cshtml  // note it is prefixed with an underscore!
    /Tenant2
        _Test.cshtml  // prefixed with an underscore too.

Then you add a little bit of logic to render the partial views...

Test.cshtml

@{
    switch(...)  // used a switch statement to illustrate the solution
    {
        case "Tenant1":
            await Html.PartialAsync("~/Pages/Tenant1/_Test.cshtml");
            break;
        case "Tenant2":
            await Html.PartialAsync("~/Pages/Tenant2/_Test.cshtml");
            break;
        default:
            throw new NotImplementedException();
    }
}

You can read about partial views here.

Extra: Using the same page model.
I also noticed that you had wanted to use the same page model (meaning sharing Test.cshtml.cs for both. This is rather trivial, but for the sake of completeness of the answer here is how you would do that...

/Pages/Test.cshtml.cs

namespace Foo.Pages
{
    public class MySharedTestModel : PageModel
    {
        ...
    }
}

/Pages/Tenant1/Test.cshtml and /Pages/Tenant2/Test.cshtml

@page
@using Foo.Pages
@model MySharedTestModel

...

这篇关于多租户 Razor 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:30