以编程方式更新 Orchard Core 租户的主机名

问题描述 投票:0回答:1

在 Orchard 管理仪表板(多租户 > 租户 > 单击所需租户的编辑)中,有一个主机名字段。我想用 C# 代码更新此字段(添加附加域)。

我可以使用 IShellSettingsManager 更改它,当我使用 IShellSettingsManager 检索值时,我会看到我的更改。但 Orchard 管理仪表板中未显示更改。当我直接在仪表板中进行更改时,它可以工作,但我需要使用代码来完成。

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using OrchardCore.Environment.Shell;
using OrchardDataApi.Services.Interface;

namespace OrchardDataApi.Services
{
    public class TenantService : ITenantService
    {
        private readonly Lazy<IShellSettingsManager> _lazyShellSettingsManager;

        private IShellSettingsManager ShellSettingsManager => _lazyShellSettingsManager.Value;

        public TenantService(IHttpContextAccessor httpContextAccessor)
        {
            _lazyShellSettingsManager = httpContextAccessor.BuildLazyService<IShellSettingsManager>();
        }

        public async Task<ShellSettings> GetShellSettingsAsync(int websiteId)
        {
            var shellSettings = await ShellSettingsManager.LoadSettingsAsync();
            return shellSettings.SingleOrDefault(x => x.Name == websiteId.ToString());
        }

        public async Task<ShellSettings> UpdateShellSettingsAsync(int websiteId, ShellSettings model)
        {
            await ShellSettingsManager.SaveSettingsAsync(model);
            return await GetShellSettingsAsync(websiteId);
        }

        public async Task<ShellSettings> AddToRequestUrlHostAsync(int websiteId, string domain)
        {
            var model = await GetShellSettingsAsync(websiteId);

            if (model != null)
            {
                var hosts = model.RequestUrlHosts.ToList();
                if (!hosts.Contains(domain))
                {
                    hosts.Add(domain);
                    model.RequestUrlHost = string.Join(',', hosts);
                    model = await UpdateShellSettingsAsync(websiteId, model);
                }
            }

            return model;
        }
    }
}
orchardcms orchardcms-1.7 orchardcore
1个回答
0
投票

你需要一个IShellHost,调用SaveSettingsAsync后,你需要这样做:

await ShellHost.ReloadShellContextAsync(model);
© www.soinside.com 2019 - 2024. All rights reserved.