C# RedirectToAction不会更改URL

C# RedirectToAction不会更改URL,c#,asp.net-mvc,asp.net-mvc-3,redirect,C#,Asp.net Mvc,Asp.net Mvc 3,Redirect,一,。 我有一个主页(主页/索引)。在这里选择语言。 这里的url是:“localhost:xxxx”。 二,。 选择语言后,以下是登录页面(帐户/索引) 这里的url是:“localhost:xxxx/Account/Index?language=en-US”。 三,。 当输入数据(用户名/密码)并单击登录按钮时,将重定向到用户/索引,但url保留在帐户/登录中 我的表格: <% using (Html.BeginForm("LogOn", "Account")) { %> <

一,。 我有一个主页(主页/索引)。在这里选择语言。 这里的url是:
“localhost:xxxx”。

二,。 选择语言后,以下是登录页面(帐户/索引) 这里的url是:
“localhost:xxxx/Account/Index?language=en-US”。

三,。 当输入数据(用户名/密码)并单击登录按钮时,将重定向到用户/索引,但url保留在帐户/登录中

我的表格:

<% 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>             
<% } %>
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
                 );
}

如何使url为:localhost:xxxx/User/Index?

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

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


因此,在提交表单时,您可以在帐户控制器上调用
登录
操作,而不是索引操作(只返回相同的视图)。

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

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

我知道这个问题已经得到了回答,但是如果您使用RedirectToRoute,而不是RedirectToAction,这会更容易,因为RedirectToRoute会强制更改url。

如果您使用jQuery mobile,请确保将
数据url
属性设置为目标页面上的正确url:

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


这就解决了问题。

Hello!。谢谢你的回答。正如你所说,我有它,它仍然给我same@user1146273,登录后操作是否命中?您的路线设置看起来如何?您是否以某种方式修改了新ASP.NET MVC应用程序向导生成的默认路由?post方法运行良好,参数获取完美。我没有用过Wizard@user1146273,那么您的路线设置在Global.asax中是什么样子的呢?我看不出您发布的代码有任何错误。它应该会起作用。我已经在我的机器上测试了它,它工作得非常好。你有一些自定义javascript可能会干扰吗?您是否安装了一些第三方NuGet软件包?问题是在使用JqueryMobile创建表单时。我不知道为什么,但最终发现了问题所在。谢谢大家!!只需将数据ajax=“false”添加到表单中,它就可以正常工作,无需任何难看的修复:)
[HttpPost]
public ActionResult LogOn(UserModel user)
{
   FormsAuthentication.SetAuthCookie(user.Username, false);
   return RedirectToActionPermanent("Index", "User");
}
<div id="my-page" data-role="page" data-url="/myurl">