@ Url.action()的ASP.NET MVC帖子 [英] ASP.NET MVC Post for @Url.action()

查看:78
本文介绍了@ Url.action()的ASP.NET MVC帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由asp.net自动生成的以下控制器

I have the following controller auto-generated by asp.net

    //
    // POST: /Account/LogOff
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut();
        return RedirectToAction("Index", "Home");
    }

现在我有一个注销按钮.当前看起来像这样:

Now I have a log off button. Currently it looks like this:

   <div class="userdrop">
                <ul>
                    <li><a href="@Url.Action("Manage", "Account")">Profile</a></li>                       
                    <li><a href="@Url.Action("LogOff", "Account")">Logout</a></li>
                </ul>
            </div><!--userdrop-->

但是它不起作用,我猜这是因为它是一种Post操作方法.

But it does not work and I am guessing it is cause it is a Post action method.

我该如何注销"?

为什么它会自动生成为Http Post?这样更安全吗?注销后它是否不发送cookie?

Why is it auto-generated as an Http Post? Is it more secure that way? Does it not send the cookie with it when it logs out?

推荐答案

我该如何注销"?

How would I go about "logging off" ?

通过使用表单而不是锚点:

By using a form instead of an anchor:

<li>
    @using (Html.BeginForm("LogOff", "Account"))
    {
        @Html.AntiForgeryToken()
        <button type="submit">Logout</button>
    }
</li>

如果需要,可以调用CSS向导来设置此按钮的样式,使其看起来像锚.但是在这种情况下,语义正确的元素是html形式,允许您发送POST动词.

You could call the CSS wizards to style this button look like an anchor if you want. But the semantically correct element in this case is an html form which allows you to send a POST verb.

这篇关于@ Url.action()的ASP.NET MVC帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆