English 中文(简体)
只有在从具体行动转向行动的情况下才能采取行动
原标题:Only allow access to action if redirected from specific action

是否有限制采取行动的好办法,因此,如果你从另一项行动转向,你只能接触这一行动。 例如:

    [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.

问题回答

了解“从”行动是没有办法的,除非你包含显示这种行动的参数。 最容易的方式是附上“行动”或“行动”参数,并在“距离”行动中加以核对。

问题是,为什么你想要这样做? 也许可以更好地解决你的首要问题。

无论如何,你可以使用HttpContext。 现时。 UrlReferer Property toeck the previous page Url.

First solution

你可以这样做:

[HttpPost]
public virtual ActionResult Create(MyViewModel vm)
{            
    if (ModelState.IsValid)
    {
        // do some work
        return this.CreateSuccess();
    }
    else
    {
        return View(vm);
    }
}

[NonAction]
public virtual ActionResult CreateSuccess()
{
    // do what s needed 
}

最后一种方法只能从其他行动方法中执行。 但它本身不能执行。

Second solution

如果你知道你可以重复使用,你也可以通过制定一种习俗行动方法来解决这个问题。 您可以撰写一种习俗行动方法,选择要求查询人的身份,并使用适当的方法。

阅读 rel=“nofollow”> 底线行动选择特性





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签