Asp.net mvc Redirect to same page after login
asp.net-mvc
Solution
Add another parameter `ReturnUrl` in the `@Html.BeginForm` which can be populated using the `ViewConext.Controller.ValueProvider`, and post the current action and controller name on which user is in the login post action like:
View:
First Way:
@using (Html.BeginForm("logon",
"Home",
FormMethod.Post,
new { ReturnUrl = Url.Action(@ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(),
@ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString()) }))
{
}
2nd Way:
@using (Html.BeginForm("logon",
"Home",
FormMethod.Post,
new { ReturnUrl = this.Request.RawUrl }))
{
}
Action:
[HttpPost]
public ActionResult logon(string tx_username, string tx_password,string ReturnUrl)
{
//If login successful
return Redirect(ReturnUrl);
}
Problem
I have a partial view which contains inputs for logging in. The partial view is present on every page of the website so that a user can login to the website from any page. I have a base controller which is then inherited by all other controllers as below. When the logon info is submitted, it goes to the logon action in the base controller. How do I return the view that the logon info was submitted from? ``` public class BaseController : Controller { [HttpPost] public ActionResult logon(string tx_username, string tx_password) { //Verify login details } } public class HomeController : BaseController { public ActionResult Index() { return View(); } public ActionResult About() { return View(); } public ActionResult Contact() { return View(); } } ```