由于您使用AJAX调用
RedirectToManageAccountaction方法,因此您有责任自己处理它的响应,并且由于
success处理函数为空,因此您实际上将忽略作为响应到达的任何内容。
如果您想从AJAX响应处理程序中强制重定向,建议
- 修改操作方法如下
[HttpPost] public ActionResult RedirectToManageAccount(string accountNumber) { var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index2", "ManageAccount", new { companyId = "7e96b930-a786-44dd-8576-052ce608e38f" }); return Json(new { Url = redirectUrl }); }- 以这种方式更新您的AJAX呼叫
self.redirectToManageAccount = function () { var accountNumber = "7e96b930-a786-44dd-8576-052ce608e38f"; $.ajax({ type: "POST", url: "/SetUpNewCompany/RedirectToManageAccount", data: { accountNumber: accountNumber }, dataType: 'json', success: function (response) { window.location.href = response.Url; }, error: function () { } }); }至于第二个问题:
另外,如何获取下面的URL,而不是带有查询字符串的URL
http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f
您只需在
RegisterRoutes()函数中为此URL定义适当的路由条目:
routes.MapRoute(null, "ManageAccount/Index2/{companyId}", new { controller = "ManageAccount", action = "Index2" } );编辑 :由于您的AJAX调用仅用于调用导致重定向的操作方法,因此您可以按照以下方式进行简化,只要此时(在客户端)您
companyId已经知道:
self.redirectToManageAccount = function () { var companyId = "12345"; window.location.href = '@(Html.ActionUri("Index2", "ManageAccount"))?companyId=' + companyId; }我在哪里使用这种扩展方法
public static string ActionUri(this HtmlHelper html, string action, string controller) { return new UrlHelper(html.ViewContext.RequestContext).Action(action, controller); }


