0

我有一个应用程序,我想制作特定国家/地区的应用程序,以便根据国家/地区为同一路线显示不同的页面。

对于每个国家/地区,我都有一个延迟加载的 RCL,带有自定义组件。我的目标是,当我转到domain.com/or时domain.com/shop,特定国家/地区的组件将从 Razor 类库加载。这意味着多个组件在 RCL 之间具有相同的路由。

举个例子:

  1. 美国:路由器将从domain.com/shop加载USApp.dll
  2. 加拿大:路由器将从domain.com/shop加载CanadaApp.dll

国家/地区存储在浏览器的本地存储中,可以随时更改。

我可以通过哪些方式实现这一目标?对于这种情况,没有关于自定义路由的文档。

4

1 回答 1

0

事实证明,解决方案非常简单。正如@MisterMagoo 所指出的,您可以随时更改其他程序集。

这就是我最终的结果:

App.razor

@using System.Reflection
@using Blazored.LocalStorage
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.WebAssembly.Services
@using Microsoft.Extensions.Logging
@inject LazyAssemblyLoader AssemblyLoader
@inject ILogger<App> Logger
@inject ILocalStorageService localStorageService

<CascadingValue Value="AppCountry">
<CascadingAuthenticationState>
    <Router AdditionalAssemblies="@lazyLoadedAssemblies" AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true" OnNavigateAsync="@OnNavigateAsync">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    @if (!context.User.Identity.IsAuthenticated)
                    {

                    }
                    else
                    {
                        <p>You are not authorized to access this resource.</p>
                    }
                </NotAuthorized>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>
</CascadingValue>

App.razor.cs

using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Client
{
    public partial class App : ComponentBase
    {
        private List<Assembly> lazyLoadedAssemblies = new();

        public string AppCountry { get; set; }

        private async Task OnNavigateAsync(NavigationContext args)
        {
            try
            {
                AppCountry = await localStorageService.GetItemAsync<string>("Country");
                Console.WriteLine(AppCountry);
                if (string.IsNullOrEmpty(AppCountry))
                {
                    AppCountry = "RO";
                }
                IEnumerable<Assembly> assemblies = Enumerable.Empty<Assembly>();
                switch (await localStorageService.GetItemAsync<string>("Country"))
                {
                    case "RO":
                        assemblies = await AssemblyLoader.LoadAssembliesAsync(
                        new[]
                           {
                           "RomanianAppLayout.dll"
                           });

                        break;

                    case "US":
                        assemblies = await AssemblyLoader.LoadAssembliesAsync(
                        new[]
                           {
                           "USAppLayout.dll"
                           });
                        break;

                    default:
                        await localStorageService.SetItemAsync("Country", "RO");
                        goto case "RO";

                }

                lazyLoadedAssemblies.Clear();
                lazyLoadedAssemblies.AddRange(assemblies);
            }
            catch (Exception ex)
            {
                Logger.LogError("Error: {Message}", ex.Message);
            }
        }
    }
}
于 2021-11-20T11:03:37.980 回答