只允许访问行动,如果从具体的操作重定向只允许、重定向、具体、行动

2023-09-03 05:08:50 作者:森浓不绿

有限制访问的操作的好方法,所以你只能访问它,如果你从另一个动作重定向。例如:

  [HttpPost]
    公共虚拟的ActionResult创建(MyViewModel VM)
    {
        如果(ModelState.IsValid)
        {
            //做一些工作

            返回RedirectToAction(CreateSuccess);
        }
        其他
        {
            返回查看(VM);
        }
    }


    公共虚拟的ActionResult CreateSuccess()
    {
        //仅允许执行,如果你是从操作重定向创建
    }
 

解决方案

一个简单的方法是将存储标志的 TempData的在第一种方法,检查标志存在于被重定向到方法。 TempData的是没有通过的行动请求之间的状态信息,并只会持续请求的时间,所以你不需要担心如何清除它。

Is there a good way to restrict the access to an action, so you can only access it, if you were redirected from another action. For example:

    [HttpPost]
    public virtual ActionResult Create(MyViewModel vm)
    {            
        if (ModelState.IsValid)
        {
            // do some work

            return RedirectToAction("CreateSuccess");
        }
        else
        {
            return View(vm);
        }
    }


    public virtual ActionResult CreateSuccess()
    {
        // only allow execution if you were redirected from Action "Create" 
    }
行啊app官方下载 行啊手机版下载v6.15.2.0 安卓最新版 当易网

解决方案

An easy way would be to store a flag in TempData in the first method and check that the flag exists in the method that is redirected to. TempData is there to pass state information between action requests and will only last the duration of the request so you will not need to worry about clearing it down.