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

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

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();");
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275011a4566286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信