/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>c# - How can I use `<MudNavLink>` as a POST `<form>` with an `<AntiforgeryToken>` in B|Concepts Of Algorithm

c# - How can I use `<MudNavLink>` as a POST `<form>` with an `<AntiforgeryToken>` in B

admin2025-04-10  2

How can I use <MudNavLink> as a POST <form> with an <AntiferyToken> in Blazor?

I have a <MudNavMenu> with several <MudNavLink>s. I want to create a logout button in the navigation menu, like the following form with an AntiferyToken:

<form>:

    <Authorized>
        <form action="authentication/logout" method="post">
            <AntiferyToken />
            <input type="hidden" name="ReturnUrl" value="@currentUrl" />
            <button type="submit" class="nav-link">
                <span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout @context.User.Identity?.Name
            </button>
        </form>
    </Authorized>

How can I achieve the above using a <MudNavLink>? I have tried to embed it in a <MudButton> of submit type, but that looks bad.

<MudNavLink>:

    <Authorized>
        <MudNavLink Href="authentication/logout" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Logout">Logout</MudNavLink>
    </Authorized>

How can I use <MudNavLink> as a POST <form> with an <AntiferyToken> in Blazor?

I have a <MudNavMenu> with several <MudNavLink>s. I want to create a logout button in the navigation menu, like the following form with an AntiferyToken:

<form>:

    <Authorized>
        <form action="authentication/logout" method="post">
            <AntiferyToken />
            <input type="hidden" name="ReturnUrl" value="@currentUrl" />
            <button type="submit" class="nav-link">
                <span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout @context.User.Identity?.Name
            </button>
        </form>
    </Authorized>

How can I achieve the above using a <MudNavLink>? I have tried to embed it in a <MudButton> of submit type, but that looks bad.

<MudNavLink>:

    <Authorized>
        <MudNavLink Href="authentication/logout" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Logout">Logout</MudNavLink>
    </Authorized>
Share Improve this question edited Mar 25 at 5:52 Qiang Fu 9,3871 gold badge6 silver badges16 bronze badges asked Mar 23 at 18:24 ShuzhengShuzheng 14.2k29 gold badges121 silver badges232 bronze badges 1
  • You could try to make the “authentication/logout" action also work for a GET request, so a form isn't needed. – Peter B Commented Mar 23 at 18:31
Add a comment  | 

1 Answer 1

Reset to default 0

You could use JS Interop to submit the form. Prevent the default behaviour of MudNavLink if you want "Match" attribute works.

@inject IJSRuntime JS

<span @onclick=SubmitLogoutForm>
    <MudNavLink Href="authentication/logout" @onclick:preventDefault="true" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Logout">Logout</MudNavLink>
</span>

    <form id="logoutForm" action="authentication/logout" method="post" style="display: none;">
        <AntiferyToken />
        <input type="hidden" name="ReturnUrl" value="currentUrl" />
    </form>

@code {
    private async Task SubmitLogoutForm()
    {
        await JS.InvokeVoidAsync("eval", "document.getElementById('logoutForm').submit();");
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744275012a239203.html

最新回复(0)