ASP.NET MVC 1.0. Controller action with the same signature

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

How can I do this since I am not passing the information back, and the method signature ends the same.

Thanks,

Mike

[Authorize] public ActionResult Index() { return View(); } [Authorize] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index() { FormsAuthentication.SignOut(); return RedirectToAction("Index", "Home"); } 
+4
source share
3 answers

Using:

 [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"); } 
+2
source

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"); } 
+5
source

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

+1
source

Source: https://habr.com/ru/post/1300092/


All Articles