【问题标题】:How to dynamically change app base path in a hosted wasm app?如何在托管的 wasm 应用程序中动态更改应用程序基本路径?
【发布时间】:2021-09-20 13:25:12
【问题描述】:

在 WASM 托管项目中,我想动态更改应用程序基本路径:给定 appSettings.json 配置文件,我想将与给定键关联的值存储在变量中,并设置 <base href="..."/>用这个变量的内容标记。

目前,我无法执行此操作,因为在 HTML 文件中我无法获取配置参数。 所以我看到了几种方法:

  • 使用javascript
  • 将 index.html(包含基本标签的文件)更改为 razor 文件

我在谷歌上搜索并将继续,但如果有人可以就如何继续给我建议,那就太好了。

【问题讨论】:

    标签: html .net-5 blazor-webassembly


    【解决方案1】:

    将 JS 文件添加到客户端应用程序。

    site.js

    
    window.SetBaseHref = function (elementId, hRef) {
        var link = document.getElementById(elementId);
        if (link !== undefined) {
            link.href = hRef;
        }
        return true;
    }
    

    wwwroot/appsettings.json

    {
      "Configuration": {
        "BaseHRef": "/blue"
      }
    }
    

    index.html 中引用它并在 <base> 上创建一个 id

        <base href="~/" id="BlazorHRef" />
    
    .......
    
        <script src="/site.js"></script>
    
    
    索引页中的演示
    @page "/"
    @page "/red/"
    
    <h1>Hello, world!</h1>
    
        Welcome to your new app.
    
    <button type="button" @onclick="this.SetBaseHRef">SetHRef</button>
    
    <SurveyPrompt Title="How is Blazor working for you?" />
    @code {
    
        [Inject] private IJSRuntime _js { get; set; }
    
        private bool _isAltHRef;
    
        private string _altHRef => _isAltHRef ? "/" : "/red";
    
        public async Task SetBaseHRef()
        {
            await this.SetBaseHref("BlazorHRef", _altHRef);
            _isAltHRef = !_isAltHRef;
        }
    
        public ValueTask<bool> SetBaseHref(string elementId, string hRef)
          => _js.InvokeAsync<bool>("SetBaseHref", elementId, hRef);
    
    }
    
    App.razor 中设置它在启动时
    <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
    
    @code {
        [Inject] IConfiguration Configuration { get; set; }
    
        [Inject] private IJSRuntime _js { get; set; }
    
        protected async override Task OnAfterRenderAsync(bool firstRender)
        {
            var baseHRef = Configuration.GetValue<string>("Configuration:BaseHRef");
            if (!string.IsNullOrEmpty(baseHRef)) await this.SetBaseHref("BlazorHRef", baseHRef);
        }
    
        public ValueTask<bool> SetBaseHref(string elementId, string hRef)
              => _js.InvokeAsync<bool>("SetBaseHref", elementId, hRef);
    
    }
    

    您可以以类似的方式更改标题中的大部分内容。

    【讨论】:

    • 你好,它几乎可以工作,但我在我的 index.html 中有一些这样的链接:&lt;link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" /&gt; 并且它们没有足够早地受到 setBaseHref 的影响,它们使用默认的 &lt;base href="~/" id="BlazorHRef" /&gt; 。你会看到这个问题的答案吗?
    • 嗯 - MudBlazor。在这一点上,你是靠自己的。 Mudblazor 的 CSS 有点古怪。我不使用它,所以无法回答您的问题。
    • 然而 MudBlazor 只是一个例子,在 index.html 中下载了许多其他 CSS 文件。 html,我认为重点是 href 开头:“_content/...”:这是一个相对地址,并且 url 是通过将相对地址附加到基本路径来构建的:“~/_content/MudBlazor/... ." 等等...问题是此时 Javascript 函数 SetBaseHref 似乎没有被调用。因此出现 404 错误。
    • 如果链接在您更改基础时发生更改,那么我唯一的答案是使用上述技术来更新链接。
    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2021-10-09
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多