RedirectToAction不会更改URL

use*_*273 3 c# asp.net-mvc redirect asp.net-mvc-3

1.我有一个主页(主页/索引).在这里您选择语言.这里的网址是这样的:"localhost:xxxx".

2.选择语言后,以下是登录页面(帐户/索引)这里的网址是: "localhost:xxxx/Account/Index?language=en-US".

3.输入数据(用户名/密码)并单击登录按钮时,重定向到用户/索引,但网址保留在帐户/登录

我的表格:

<% 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>             
<% } %>
Run Code Online (Sandbox Code Playgroud)

账户管理员:

[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();
  }
}
Run Code Online (Sandbox Code Playgroud)

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
                 );
}
Run Code Online (Sandbox Code Playgroud)

如何制作网址是:localhost:xxxx/User/Index?

Dar*_*rov 5

在您的Account/Index.cshtml视图中替换:

@using (Html.BeginForm())
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

有:

@using (Html.BeginForm("LogOn", "Account"))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

这样LogOn当您提交表单而不是索引操作(它只返回相同的视图)时,您将在帐户控制器上调用操作.


Dra*_*nov 5

在您的登录中使用永久重定向:

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