只允许访问行动,如果从具体的操作重定向 [英] Only allow access to action if redirected from specific action

查看:173
本文介绍了只允许访问行动,如果从具体的操作重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  [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" 
    }

解决方案

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.

这篇关于只允许访问行动,如果从具体的操作重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆