ASP.NET MVC 1.0控制器动作与相同签名

4

基本上,在我的UserController.cs类中,我有一个Index方法,它返回ActionResult以显示用户的仪表板。在此页面上有一个类型为submit的html按钮。当我点击此按钮时,我想在服务器端捕获它并注销用户。

由于我没有传递信息,所以我该如何做到这一点,而方法签名最终相同。

谢谢,

麦克

    [Authorize]
    public ActionResult Index()
    {
        return View();
    }

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
3个回答

5

使用 ActionNameAttribute

[AcceptVerbs(HttpVerbs.Get), ActionName("Index"), Authorize]
public ActionResult IndexGet()
{
    return View();
}

[AcceptVerbs(HttpVerbs.Post), ActionName("Index"), Authorize]
public ActionResult IndexPost()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

2

使用:

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection values)
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

或者

[Authorize]
[AcceptVerbs(HttpVerbs.Post), ActionName("Post")]
public ActionResult IndexPost()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

1

正如您所知,在C#中,您不能在同一类中使用相同名称和相同参数的两个方法。您可以添加一些虚拟参数,或者我建议您将第二个操作重命名为SignOut,这似乎更符合语义,并更好地反映了此操作实际执行的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接