尝试使用 ABP.io 框架 3.1 设置 Multi-Tenancy 站点。
我正在尝试在页面 html 头部设置 <meta 关键字(以及其他标签)。我正在尝试从当前租户的数据库字段中获取值,因此元关键字将特定于租户。 我尝试遵循此处提供的示例:https://docs.abp.io/en/abp/latest/UI/AspNetCore/Customization-User-Interface#layout-hooks他们将谷歌分析脚本代码注入(inject)到 head 标签中。
这很好,因为它是静态文本,但是当我尝试使用模型加载部分页面时,它会抛出一个错误,提示需要与传入的模型不同的模型。
到目前为止我已经有了通知 View 组件
Public class MetaKeywordViewComponent : AbpViewComponent
{
public async Task<IViewComponentResult> InvokeAsync() {
return View("/Pages/Shared/Components/Head/MetaKeyword.cshtml"); //, meta);
}
}
和cshtml页面
@using MyCompany.MyProduct.Web.Pages.Shared.Components.Head
@model MetaKeywordModel
@if (Model.SiteData.Keywords.Length > 0)
{
<meta content="@Model.SiteData.Keywords" name="keywords" />
}
cshtml.cs 文件为
public class MetaKeywordModel : MyProductPageModel
{
private readonly ITenantSiteDataAppService _tenantSiteDataAppService;
public TenantSiteDataDto SiteData { get; private set; }
public MetaKeywordModel(ITenantSiteDataAppService tenantSiteDataAppService)
{
_tenantSiteDataAppService = tenantSiteDataAppService;
}
public virtual async Task<ActionResult> OnGetAsync()
{
if (CurrentTenant != null)
{
SiteData = await _tenantSiteDataAppService.GetSiteDataAsync();
}
return Page();
}
}
但是当我运行该程序时,出现以下错误。
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Volo.Abp.AspNetCore.Mvc.UI.Components.LayoutHook.LayoutHookViewModel', but this ViewDataDictionary instance requires a model item of type 'MyCompany.MyProduct.TenantData.Dtos.TenantSiteDataDto'.
如果我无法使用我的模型,如何将数据从数据库传递到要呈现的页面?
任何帮助提示或技巧将不胜感激。
问候 马蒂
最佳答案
ViewComponent 与 razor 页面不同。
您应该直接将服务注入(inject) View 组件类中。像:
public class MetaKeywordViewComponent : AbpViewComponent
{
private readonly ITenantSiteDataAppService _tenantSiteDataAppService;
public MetaKeywordViewComponent(ITenantSiteDataAppService tenantSiteDataAppService)
{
_tenantSiteDataAppService = tenantSiteDataAppService;
}
public async Task<IViewComponentResult> InvokeAsync()
{
return View("/Pages/Shared/Components/Head/MetaKeyword.cshtml",
await _tenantSiteDataAppService.GetSiteDataAsync());
}
}
关于abp - 如何将包含数据的模型从数据库传递到 ABP.IO 布局 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63821517/