Ben*_*eld 7 asp.net-mvc multi-tenant .net-core razor-pages
我目前正在尝试在 dotnetcore 2.1 中使用 Razor Pages 实现一个主题化网站,但我对页面无法加载的原因有一些麻烦/困惑。
对站点的每个请求都会导致根据访问的域设置主题值,默认情况下,主题是“默认”,它存储在每个请求的 RouteData 中。
我已经实施了以下 ThemeViewLocationExpander
public class ThemeViewLocationExpander : IViewLocationExpander
{
private const string ValueKey = "Theme";
public void PopulateValues(ViewLocationExpanderContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
context.Values[ValueKey] = (context.ActionContext.RouteData.Values["tenant"] as Tenant)?.Theme;
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (viewLocations is null)
{
throw new ArgumentNullException(nameof(viewLocations));
}
context.Values.TryGetValue(ValueKey, out string theme);
if (!string.IsNullOrEmpty(theme))
{
viewLocations = new[] {
$"/Pages/Themes/{theme}/{{1}}/{{0}}.cshtml",
$"/Pages/Themes/{theme}/Shared/{{0}}.cshtml",
$"/Pages/Shared/{{0}}.cshtml",
};
}
return viewLocations;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在尝试从 ... 加载页面(因为每个主题可能具有完全不同的布局,而不仅仅是不同的 CSS),/Pages/Themes/{THEME_NAME}
而不是/Pages/
,除了/Pages/Shared
用于验证部分等内容的顶级共享文件夹可能存在于所有主题中的脚本。
我不希望直接有任何其他页面,/Pages
例如/Pages/Index.cshtml
每个请求都应该最终出现在一个主题文件夹中,因为必须有一个主题。
我已将以下内容添加到我的 Startup.cs
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new ThemeViewLocationExpander());
});
Run Code Online (Sandbox Code Playgroud)
我可以在第一次加载时断点此代码,所以我知道它已注册,但是如果我运行该站点并在https://localhost:44352/
应该加载/Pages/Index.cshtml
时加载它/Pages/Themes/Default/Index.cshtml
。
如果我删除/Pages/Index.cshtml
并运行该网站,我会得到
This localhost page can’t be found
No webpage was found for the web address: https://localhost:44352/
HTTP ERROR 404
Run Code Online (Sandbox Code Playgroud)
任何人都可以为我提供任何见解或帮助吗?
也许我需要修改一些路由来合并主题,或者我错过了一些重要的东西,似乎我在 中设置的视图位置ThemeViewLocationExpander.ExpandViewLocations()
从未被使用过。
我已经阅读了很多关于 DNC 2.1 的多租户内容,但它要么用于 MVC 而不是 RazorPages,要么仅在主题文件夹内实现 CSS / _Layout.cshtml 页面并使用默认页面/Pages/
进行布局(我正在尝试每个主题文件夹中的所有页面都有单独的实现)
编辑:
我决定将/Pages/Index.cshtml
这些页面和其他页面保留在顶级文件夹中并将其用作默认的“主题”会更有意义,然后任何新主题都将使用这些页面,除非在其/Pages/Themes/{THEME_NAME}
文件夹内特别覆盖。
我已经实现了这一点,但我仍然不知道为什么/Pages/Index.cshtml
即使设置了主题值并且viewLocations
将主题文件夹添加到了页面加载。
编辑2:
已经对其进行了测试,并且使用主题集,它肯定在主题文件夹中使用了正确的布局页面。例如,当我加载https://localhost:44352/
布局页面时,主题为 'Fresh' /Pages/Themes/Fresh/Shared/_Layout.cshtml
,但从notIndex.cshtml
加载。/Pages/Index.cshtml
/Pages/Themes/Fresh/Index.cshtml
在这一点上viewLocations
是:
/Pages/Themes/Fresh/{1}/{0}.cshtml
/Pages/Themes/Fresh/Shared/{0}.cshtml
/Pages/Shared/{0}.cshtml
/Pages/{1}/{0}.cshtml
/Pages/Shared/{0}.cshtml
/Views/Shared/{0}.cshtml
Run Code Online (Sandbox Code Playgroud)
编辑3:
这是该项目的简化版本的 github 链接,它演示了我遇到的问题。https://github.com/BenMaxfield/ThemeTest
为了测试它,只需运行应用程序,您应该看到它加载了 DARK 主题布局,但加载了 DEFAULT Index 内容。
要切换到 DEFAULT 主题,请查找PopulateValues
方法ThemePageViewLocationExpander
并设置context.Values[ThemeKey] = string.Empty;
编辑 4
我注意到这ThemePageViewLocationExpander.ExpandViewLocations
只是对部分文件的调用,而不是对带有代码的实际页面。
这解释了为什么_Layout.cshtml
为主题加载了正确的页面,但Index.cshtml
没有加载正确的文件(就像/Pages/Index.cshtml
一个带有代码的页面并且有@page
声明 - 即它不是部分的)
为了解决这个问题在我创建了一个新的文件夹哈克的方式/Pages/Views/
,把一个部分.cshtml
对应于每一个的@page
在顶级页/Pages/
,
例如我现在有/Pages/Index.cshtml
(非部分)和/Pages/Views/Index.cshtml
(部分)。
在里面/Pages/Index.cshtml
我添加<partial name="Index" model="@Model"/>
了从新/Pages/Views/
文件夹加载相应的部分。
由于现在正在加载一个部分ThemePageViewLocationExpander.ExpandViewLocations
被调用,然后我可以根据给定的主题键(如果存在)覆盖它的视图位置。
这意味着所有的请求/Index
将加载OnGet()
从/Pages/Index.cshtml
随后加载在其看来,我可以任选地通过添加另一个覆盖部分Index.cshtml
的局部内部/Themes/{THEME_NAME}/Views/
。
这是我当前的视图位置覆盖 ThemePageViewLocationExpander.ExpandViewLocations
viewLocations = new[] {
$"/Themes/{theme}/{{0}}.cshtml",
$"/Themes/{theme}/Views/{{0}}.cshtml",
$"/Pages/Views/{{0}}.cshtml",
}.Concat(viewLocations);
Run Code Online (Sandbox Code Playgroud)
以及一个示例文件夹结构:
.
??? Pages
| ??? Index.cshtml (with code behind, called on localhost:45635/)
| ??? Views
| ??? Index.cshtml (partial)
??? Themes
| ??? Dark
| ??? Views
| ??? Index.cshtml (partial)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1111 次 |
最近记录: |