how to lock an asp.net mvc action?

后端 未结 6 673
情话喂你
情话喂你 2020-12-01 05:46

I\'ve written a controller and action that I use as a service. This service runs quite a costly action. I\'d like to limit the access to this action if there is already a cu

6条回答
  •  难免孤独
    2020-12-01 05:55

    The simplest way to do that would be save to the cache a Boolean value indicating the action is running the required BL already:

    if (System.Web.HttpContext.Current.Cache["IsProcessRunning"])
    {
        System.Web.HttpContext.Current.Cache["IsProcessRunning"] = true;
        // run your logic here
        System.Web.HttpContext.Current.Cache["IsProcessRunning"] = false
    }
    

    Of course you can do this, or something similar, as an attribute as well.

提交回复
热议问题