RedirectToAction not changing URL or navigating to Index view

Viewed 85178

I tried using a RedirectToAction after I have done a post to the controller and saved but the URL does not change and the redirect does not seem to work. I need to add that the redirect does enter the controller action method when I debug. It does not change the URL or navigate to the Index view.

public ViewResult Index()
{
    return View("Index", new TrainingViewModel());
}

public ActionResult Edit()
{
    // save the details and return to list
    return RedirectToAction("Index");    
}

What am I doing wrong?

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{resource}.js/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = "" } // Parameter defaults
    );
}

jQuery calls:

this.SynchValuesToReadOnly = function() {
    window.location = $('#siteRoot').attr('href') + 'Sites/';           
};

this.OnSiteEdit = function() {
    // post back to the server and update the assessment details    
    var options = {
        target: '',
        type: 'post',
        url: '/Site/Edit',
        beforeSubmit: othis.validate,
        success: othis.SynchValuesToReadOnly
    };

    $('#uxSiteForm').ajaxSubmit(options);
};
8 Answers

The code you show is correct. My wild guess is that you're not doing a standard POST (e.g., redirects don't work with an AJAX post).

The browser will ignore a redirect response to an AJAX POST. It's up to you to redirect in script if you need to redirect when an AJAX call returns a redirect response.

Check with firebug. Post might be returning a server error.

I decided to write this comment as a new answer because I think it needs more description and may someone can't see it in comments if has my problem. So I Apologize if you think this is not an answer and must be written in the comment section.

In some cases, I used RedirectToAction to send the user to another action if input values are incorrect or insufficient and avoid continuing the current process as below:

if (inputValues == null)
{
  RedirectToAction("index");
}

That keeps not working and continues the current process to take unhandled error. I search and read some articles but I can't find a solution. Calling of RedirectToAction is not from Ajax code and routing is so simple without any parameters even. So why is not working? Finally, I found the simple mistake I did: Forgot to put "return" keyword before RedirectToAction. I changed the code as below:

if (inputValues == null)
{
  return RedirectToAction("index");
}

And that is worked. Hope this help.

I just came across this issue and nothing was working for me. It turned out the controller I was redirecting to required Authorization - the [Authorize] attribute was preventing any redirection

   [Authorize]//comment this out
   public class HomeController : Controller {...}

Because I forgot to add app.UseAuthentication() to the code block in the Startup.cs.

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
   ...
   app.UseHttpsRedirection();
   app.UseStaticFiles();
   app.UseAuthentication();
   app.UseMvc(routes =>
   ...

After adding the redirect began to work

Related