Asp.Net Core重定向到操作但不允许直接调用操作?

kno*_*ker 6 c# asp.net asp.net-mvc asp.net-core-mvc asp.net-core

我有一种情况,我希望在结果成功时重定向/显示某个url/action但是如果有错误则返回查看.

例如,当someWork返回true时,我想用一些数据显示"成功页面",但是当它为false时,我将返回页面并显示错误.

通常ChildAction可以做到,但在.Net Core中,它们似乎缺失了.

实现这一目标的最佳方法是什么?我主要担心的是,如果有人在浏览器栏中将其写入,则不应直接访问"成功"路线/操作.

public IActionResult DoSomething()
{
    bool success = someWork();
    if (success)
    {
       // goto some action but not allow that action to be called directly
    }
    else
    {
       return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

Ema*_*mad 3

一种解决方案(或者更确切地说是一种解决方法)是使用临时数据来存储布尔值并在其他操作中检查它。像这样:

public IActionResult DoSomething()
{
    bool success=someWork();
    if(success)
    {
        TempData["IsLegit"] = true;
        return RedirectToAction("Success");
    }
    else
    {
        return View();
    }
}

public IActionResult Success
{
    if((TempData["IsLegit"]??false)!=true)
        return RedirectToAction("Error");
    //Do your stuff
}
Run Code Online (Sandbox Code Playgroud)