[英]ASP.NET MVC - Does not redirect in HttpPost method with RedirectToAction
我在使用RedirectToAction
方法時遇到問題。 單擊按鈕后,它停留在當前視圖中。 在我的瀏覽器上打開 PAYLOAD 並檢查 .network 后,我看到它以狀態代碼 200 重定向,在預覽中,重定向后正確的 HTML 在那里,但它沒有重定向。! 我已經搜索了將近一天,似乎沒有人能夠給出正確答案。
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()
}
}
}
}
我認為這與以某種方式阻止重定向到另一個頁面的HttpPost
方法有關。 當我嘗試將RedirectToAction
放在上面的 View() ActionResult 中時,它起作用了:
public ActionResult Test()
{
return RedirectToAction("SuccessPage", "Success")
}
但這不是我想要的。 我希望它在執行 POST 請求后重定向。 我的 POST 請求有效,因為它給了我想要的 boolean 結果。 請幫忙!!
考慮到它在您嘗試將 RedirectToAction 放在上面的 View() ActionResult 中時有效。我認為您的 SuccessPage return View() 方法丟失了。 如果這對您沒有意義,我將解釋它的含義,基本上 RedirectToAction(string actionName) 通過調用返回相應視圖的相應 actionName 來工作。 在您的情況下,解決方案是添加另一種方法
public ActionResult SuccessPage()
{
return View();
}
就在 Test() 下面,然后嘗試。 我希望它能適用於你的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.