2017-05-28 31 views
0

我有2個POST動作,每一個想要從一個重定向到另一個:重定向從POST操作到POST操作

[HttpPost] 
public IActionResult Foo() 
{ 
    bool isBar = handleFoo(); 
    if (isBar) return RedirectToAction("Bar"); 
    else return View(); 
} 


[HttpPost] 
public IActionResult Bar() 
{ 
    bool isFoo = handleBar(); 
    if (isFoo) return RedirectToAction("Foo"); 
    else return View(); 
} 

Assumedly,用戶提交一個表單,並運行Bar方法中,如果一個布爾表達式isFoo爲真,則用其視圖返回Foo方法,否則返回當前的Bar視圖。

目前,我的代碼無法爲動作返回正確的視圖,我該怎麼做?

+2

'RedirectToAction'是GET,而不是一個POST –

+0

化妝FOO as httpGet – Saurabh

+0

@Saurabh它們都是POST方法。 – MiP

回答

0

的方法也請指定視圖名稱您案例:

[HttpPost] 
public IActionResult Foo() 
{ 
    bool isBar = handleFoo(); 
    if (isBar) return Bar(); 
    else return View("Foo"); 
} 


[HttpPost] 
public IActionResult Bar() 
{ 
    bool isFoo = handleBar(); 
    if (isFoo) return Foo(); 
    else return View("Bar"); 
} 
0

RedirectToAction使用GET不能郵寄,所以我會假設,在同一個控制器的兩個動作,所以你可以調用其他行動調用這樣

[HttpPost] 
public IActionResult Foo() 
{ 
    bool isBar = handleFoo(); 
    if (isBar) return Bar(); 
    else return View(); 
} 


[HttpPost] 
public IActionResult Bar() 
{ 
    bool isFoo = handleBar(); 
    if (isFoo) return Foo(); 
    else return View(); 
} 
+0

這就是我所做的,但它返回錯誤的視圖,即'Foo()'返回'Bar()',但仍然獲得Foo的視圖。 – MiP

+0

什麼是handleFoo和HandleBar方法? –

+0

它在當前的例子中並不重要,你可以認爲它返回Random.NextDouble()> 0.5'。 – MiP

0

希望我有你的問題的解決方案,代碼應該看起來像這樣。

​​

注意:請作出FOO()方法的action屬性爲[HTTPGET]。

由於RedirectToAction()僅支持get請求,ie.it就像是從瀏覽器提供新的請求。

而對於Bar()方法,您可以保留actionattribute爲[httppost],因爲它是從窗體調用的。

希望以上信息對您解決問題是有用的,請讓我知道你的想法或反饋

感謝

KARTHIK

+0

他們都是POST方法。 – MiP

+0

然後嘗試下面的鏈接,看看它是否工作:https://stackoverflow.com/a/3740110/3397630 –

+0

這可以工作,但恐怕ASP.NET會選擇'GET',我不想揭露URL中的urgly查詢,而不是我在尋找的內容。順便說一句,.NET核心中的正確屬性應該是'[AcceptVerbs(「Get」,「Post」)]' – MiP