Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction does not change the URL

1. I have a home page (Home / Index). Here you select the language. Here the url is this: "localhost:xxxx".

2. After selecting the language, the following is a login page (Account / Index) Here the url is this: "localhost:xxxx/Account/Index?language=en-US".

3. When entering data (Username / Password) and click on the Logon button, redirects to User / Index but the url stays in Account/LogOn

My Form:

<% using (Html.BeginForm("LogOn", "Account")) { %>
<div data-role="fieldcontain" class="ui-hide-label">
  <label for="username">Username:</label>
  <%: Html.TextBoxFor(m => m.Username, new { placeholder = "Username" })%>                      
</div>
<div data-role="fieldcontain" class="ui-hide-label">
  <label for="password">Password:</label>
  <%: Html.PasswordFor(m => m.Password, new { placeholder = "Password" })%>                 
</div>
<fieldset class="ui-grid-a">
  <div class="ui-block-a"><button type="reset" data-theme="d">Reset</button></div>
  <div class="ui-block-b"><button type="submit" data-theme="b">Log On</button></div>
</fieldset>             
<% } %>

Account Controller:

[HandleError]
public class AccountController : Controller
{        
  public ActionResult Index(string language = "es-Es")
  {
    return View();
  }

  [HttpPost]
  public ActionResult LogOn(UserModel user)
  {
    FormsAuthentication.SetAuthCookie(user.Username, false);
    return RedirectToAction("Index", "User");
  }

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

Global.asax:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
                  "Default", // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                 );
}

How to make the url is: localhost:xxxx/User/Index ?

like image 745
user1146273 Avatar asked Dec 28 '22 07:12

user1146273


2 Answers

In your LogOn use permanent redirect:

[HttpPost]
public ActionResult LogOn(UserModel user)
{
   FormsAuthentication.SetAuthCookie(user.Username, false);
   return RedirectToActionPermanent("Index", "User");
}
like image 110
Draganov Avatar answered Dec 29 '22 21:12

Draganov


In your Account/Index.cshtml view replace:

@using (Html.BeginForm())
{
    ...
}

with:

@using (Html.BeginForm("LogOn", "Account"))
{
    ...
}

so that you invoke the LogOn action on the Account controller when you submit the form and not the Index action (which simply returns the same view).

like image 22
Darin Dimitrov Avatar answered Dec 29 '22 22:12

Darin Dimitrov