user1146273
user1146273

Reputation: 55

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 ?

Upvotes: 4

Views: 17120

Answers (4)

rickythefox
rickythefox

Reputation: 6851

If you use jQuery mobile make sure you have the data-url attribute set to the correct url on the target page:

<div id="my-page" data-role="page" data-url="/myurl">

This solves the problem.

Upvotes: 0

Esteban
Esteban

Reputation: 3139

I know this question has been answered already, but it'd be easier if you use RedirectToRoute, instead RedirectToAction, it turns out that RedirectToRoute forces the url change.

Upvotes: 0

Draganov
Draganov

Reputation: 375

In your LogOn use permanent redirect:

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

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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).

Upvotes: 5

Related Questions