2016-12-27 3 views
0

Ich benutze asp.net Core die Rasierer-Engine. Ich bin auf eine andere Funktion umleiten und es funktionierte nicht, wenn die Funktion, an die ich es schickte, eine HttpPost war. Als ich es zu HttpGet änderte, hat alles gut funktioniert. Warum ist das?Funktioniert RedirectToAction nur für HttpGet?

Hier ist mein Code

static bool init = false; 
// GET: /Home/ 
[HttpGet] 
[Route("")] 
public IActionResult Index() 
{ 
    if(init == false){ 
     HttpContext.Session.SetString("Happiness", "190"); 
     HttpContext.Session.SetString("Fullness", "190"); 
     HttpContext.Session.SetString("Energy", "190"); 
     HttpContext.Session.SetString("Meal", "3"); 
     HttpContext.Session.SetString("Message", ""); 
     HttpContext.Session.SetString("Tree", "/images/regular_tree.jpeg"); 
     init = true; 
    } 
    ViewData["Tree"] = HttpContext.Session.GetString("Tree"); 
    ViewData["Happiness"] = HttpContext.Session.GetString("Happiness"); 
    ViewData["Fullness"] = HttpContext.Session.GetString("Fullness"); 
    ViewData["Energy"] = HttpContext.Session.GetString("Energy"); 
    ViewData["Meal"] = HttpContext.Session.GetString("Meal"); 
    ViewData["Message"] = HttpContext.Session.GetString("Message"); 

    if(Int32.Parse(HttpContext.Session.GetString("Happiness")) >= 100 && 
    Int32.Parse(HttpContext.Session.GetString("Fullness")) >= 100 && 
    Int32.Parse(HttpContext.Session.GetString("Energy")) >= 100){ 
     System.Console.WriteLine("WinFunction"); 
     System.Console.WriteLine("WinTwice"); 
     return RedirectToAction("Win"); 
    } 
    return View(); 
} 

[HttpGet] 
[Route("win")] 
public IActionResult Win() 
{ 
    System.Console.WriteLine("Win"); 
    return View(); 
} 
+0

[Diese Seite] (http://stackoverflow.com/questions/129335/how-do-you-redirect-to-a-page-using-the-post-verb#answer-1343182) enthält eine interessante Problemumgehung dass Sie möglicherweise verwenden können. –

Antwort

2

Die Aussage return RedirectToAction wird eine 302-Antwort an den Browser mit der neuen URL im Location-Header senden, und Browser wird für diese URL eine neue GET-Anforderung ausgeben. Wenn Sie action-Methode nur mit HttpPost Attribut markiert ist, wird es für eine GET-Anfrage nicht funktionieren. Kurz gesagt, Ihre Aktion Win sollte nicht mit der Aktion [HttpPost] markiert werden.

+0

Diese Antwort führt nicht zur Ursache, nämlich dass HTTP die Umleitung mit POST nicht unterstützt. –