简体   繁体   中英

ASP.NET MVC - Does not redirect in HttpPost method with RedirectToAction

I am having issues working with the RedirectToAction method. After clicking on the button, it stays in the current View. After opening the PAYLOAD on my browser and checking the.network, I see that it redirected with a status code of 200 and in the preview, the correct HTML after redirecting was there but it does not redirect.! I have been searching for almost a day and no one seems to be able to give the correct answer.

namespace MVC.Controllers
{
    public class TestController: Controller
    {
        public ActionResult Test()
        {
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> Test(string test)
        {
            using (Stream iStream = Request.InputStream)
            {
                using (StreamReader reader = new StreamReader(iStream, Encoding.UTF8))
                {
                    string requestData = await reader.ReadToEndAsync();
                    Service1Client o = new Service1Client();

                    bool result = o.Test(requestData);  // returns true

                    if (result == true) {
                        // It showed status 302 and the html in the payload was the correct one with status code 200 but it stays on the same page 
                        return RedirectToAction("SuccessPage", "Success")
                    } else {
                        return View()
                    }
                }
            }
        }

I think it is something to do with the HttpPost method that somehow prevents redirecting to another page. When I tried putting the RedirectToAction inside the View() ActionResult above, it worked:

        public ActionResult Test()
        {
            return RedirectToAction("SuccessPage", "Success")
        }

but this is not what I want. I want it to redirect after performing a POST request. My POST Request works because it gave me the boolean result I want. Please help!!

Well considering that it works when you tried putting the RedirectToAction inside the View() ActionResult above.. I think your SuccessPage return View() method is missing. If that doesn't make sense to you, I'll explain what it means, Basically RedirectToAction(string actionName) works by calling the respective actionName that returns the respective View. In your case, the solution will be adding another method

public ActionResult SuccessPage()
{
    return View();
}

Right below the Test() And then try. I hope it'll work in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM