0

suppose i have two action in controller called login

public ActionResult Login()    
{    
    return View();    
}    

[HttpPost]    
[ValidateAntiForgeryToken]    
public ActionResult Login(UserProfile objUser)     
{    
    if (ModelState.IsValid)     
    {    
    using(DB_Entities db = new DB_Entities())    
    {    
        var obj = db.UserProfiles.Where(a => a.UserName.Equals(objUser.UserName) && a.Password.Equals(objUser.Password)).FirstOrDefault();    
        if (obj != null)    
        {    
        Session["UserID"] = obj.UserId.ToString();    
        Session["UserName"] = obj.UserName.ToString();    
        return RedirectToAction("UserDashBoard");    
        }    
    }    
    }    
    return View(objUser);    
}   

when we write return RedirectToAction("Login"); then how asp.net mvc understand that we are thinking about login function which uisng get http verb login not post http verb login ? anyone can explian it ?

how we can redirect to action which is based on post http verb ? what will be the syntax if we need to redirect to action with http verb if we use RedirectToAction function ?

thanks

  • 9
    A redirect response always results in the browser issuing a `GET` request to the redirected address. If you want the browser to issue a `POST` request you'd likely do that from client-side code by submitting a form. – David Jun 02 '17 at 11:29
  • 1
    From https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx#M:System.Web.Mvc.Controller.RedirectToAction(System.String), "...Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action...." – Jose Luis Jun 02 '17 at 11:32
  • how to call a post action from another get or post action. the post action we will call will return a view to client. so is there any in built function which i can use to call post action ? –  Jun 02 '17 at 11:42
  • You can use HTTP Client for it: https://stackoverflow.com/a/15176685/1662459 – G J Jun 02 '17 at 11:44
  • 1
    RedirectToAction always pointed to the http get. – Dipak Delvadiya Jun 02 '17 at 12:38
  • If the original request is a POST, and you want to redirect to a POST, you likely want a 307. Have a squiz at https://stackoverflow.com/a/27435173/34092 . Also see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection . – mjwills Jun 02 '17 at 13:36
  • if you really want this way , you can just try by calling the method directly with appropriate parameter. here is the example:https://stackoverflow.com/questions/16643414/asp-net-mvc-redirecttoaction-with-parameters-to-post-action – Karthik Elumalai Jun 02 '17 at 16:22

1 Answers1

0

You can user above action as a different way like this.

[HttpPost]    
[ValidateAntiForgeryToken]    
public ActionResult Login(UserProfile objUser)     
{    
    // Code;    
}   

[HttpGet]    
[ValidateAntiForgeryToken]    
public ActionResult Login(UserProfile objUser)     
{    
    // Code;    
}  
NIts577
  • 420
  • 3
  • 13