C# Net MVC AJAX调用返回到原始actionMethod,而不是新的url

C# Net MVC AJAX调用返回到原始actionMethod,而不是新的url,c#,asp.net-mvc,C#,Asp.net Mvc,我想做的事可能吗?看起来应该是这样。 我希望看到MyActionMethod继续运行到新的url,但它最终被再次调用 我已将其简化,以深入到本质。我的动作方法是从一个带有Ajax.BeginForm()的视图中调用的,一旦提交,我就可以在控制器中看到它 public ActionResult MyActionMethod(MyViewModel myViewModel) { isRedirectNeeded = true; return RedirectI

我想做的事可能吗?看起来应该是这样。 我希望看到MyActionMethod继续运行到新的url,但它最终被再次调用

我已将其简化,以深入到本质。我的动作方法是从一个带有Ajax.BeginForm()的视图中调用的,一旦提交,我就可以在控制器中看到它

public ActionResult MyActionMethod(MyViewModel myViewModel)
    {
        isRedirectNeeded = true;
        return RedirectIfNeeded(isRedirectNeeded)   
    }
确定重定向到何处的方法。我有一个方法,因为它被调用了几次,并且比本例中的方法有更多的目的地

  private ActionResult RedirectIfNeeded(bool isRedirectNeeded)
    {
        if (isRedirectNeeded)
        {
            return Redirect(@"~/myController/MyMethod"); 
        }
        else 
        {
            return Json("<div class='alert alert-error'>" + errorMessage + "</div>");
        }
    }
我现在在谷歌上搜索,但是我想把这些信息发布出去,这样你们就有了我所知道的所有信息


答案在上面重复评论中提到的问题中。

您需要使用return RedirectToAction();要重定向到其他操作

我不会重定向到其他操作。“RedirectIfNeeded()”只是控制器中返回ActionResult的一个私有方法。在使用ajax时不能以这种方式重定向,需要将url返回为json并使用javascript重定向。我相信发送http状态代码并在javascript中相应地执行操作是一种很好的做法。谢谢@Erik Funkenbush,当我第一次写这个问题时,我没有意识到这是一个AJAX问题。
    protected void Application_EndRequest()
    {
        var context = new HttpContextWrapper(Context);
        if (context.Request.IsAjaxRequest() && context.Response.StatusCode == 302)
        {
            Context.Response.Clear();
            Context.Response.Write("**custom error message**");
            Context.Response.StatusCode = 401;
        }
    }