RedirectToAction not working in a particular actio

2019-06-25 13:44发布

问题:

I have an Ajax call which will call an action called submit on form submission in a controller..

In that is something like below:

if (CSConfigurationMgr.IsMobileUrl(Request.UrlReferrer.AbsoluteUri.PathFromURL()))
    return RedirectToAction("home", "mobile", new { success = true});

It redirects to homepage and displays a "thank you" message.

I tried just giving url as xxxxxxx/mobile/home?success=true and it worked as expected.

I am getting true for that condition and going to return statement but i am not being redirected to the new page.

I've checked routes and found no problems there.

I checked by placing the RedirectToAction in another action, it worked fine, I am redirected to new page. But it is not working here.

Errors:

  • Cannot redirect after HTTP headers have been sent: Arises sometimes (mobile browsers)
  • Nothing happens, just stays on the same page

Moreover, I tried to debug in firebug. After the call is made I get the process:

www.test.com is nothing but my localhost and its port number

POST http://www.test.com/forms/submit which is the right path to pass data

GET http://www.test.com/mobile/home?success=true the path I have to redirect to. It keeps showing the loading sign, doesn't move further from there in firebug.

I suspect that the request is being made but not redirecting.

回答1:

The error "Cannot redirect after HTTP headers have been sent" means that you've already started returning the HTTP response to the client. Therefore you can't redirect it now. Look earlier in your controller method to see if it returns anything to the client and move the redirect check earlier.

The only other thing that looks odd is that you have the view before the controller (default is /{controller}/{view}), but perhaps your routes are set up that way.