tags:

views:

36

answers:

3

Hi everyone,

So basically in my UserController.cs class I have an Index method that returns the ActionResult to display the dashboard for the user. On this page is a html button with a type of submit. When I hit this button I want to capture it on the server side log the user out.

How can I do this since I'm not passing information back and the method signature ends up being the same.

Thanks,

Mike

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

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
+1  A: 

Use:

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

Or

[Authorize]
[AcceptVerbs(HttpVerbs.Post), ActionName("Post")]
public ActionResult IndexPost()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}
John Gietzen
Thanks for the quick response.
Mike
A: 

As you know in C# you cannot have two methods with the same name and same arguments within the same class. You could either add some dummy parameter or I would advice you to rename the second action to SignOut which seems more semantically correct and better reflecting what this action actually does.

Darin Dimitrov
+1  A: 

Use 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");
}
Craig Stuntz