简体   繁体   English

ASP.NET MVC - 不使用 RedirectToAction 在 HttpPost 方法中重定向

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

I am having issues working with the RedirectToAction method.我在使用RedirectToAction方法时遇到问题。 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.!在我的浏览器上打开 PAYLOAD 并检查 .network 后,我看到它以状态代码 200 重定向,在预览中,重定向后正确的 HTML 在那里,但它没有重定向。! 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.我认为这与以某种方式阻止重定向到另一个页面的HttpPost方法有关。 When I tried putting the RedirectToAction inside the View() ActionResult above, it worked:当我尝试将RedirectToAction放在上面的 View() ActionResult 中时,它起作用了:

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

but this is not what I want.但这不是我想要的。 I want it to redirect after performing a POST request.我希望它在执行 POST 请求后重定向。 My POST Request works because it gave me the boolean result I want.我的 POST 请求有效,因为它给了我想要的 boolean 结果。 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.考虑到它在您尝试将 RedirectToAction 放在上面的 View() ActionResult 中时有效。我认为您的 SuccessPage return View() 方法丢失了。 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.如果这对您没有意义,我将解释它的含义,基本上 RedirectToAction(string actionName) 通过调用返回相应视图的相应 actionName 来工作。 In your case, the solution will be adding another method在您的情况下,解决方案是添加另一种方法

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

Right below the Test() And then try.就在 Test() 下面,然后尝试。 I hope it'll work in your case.我希望它能适用于你的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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