[英]How to check sessions on every action of a controller?
i have a controller called AllEntities
it has multiple actions like GetEntityByTypeId
and GetLastEntity
我有一个名为
AllEntities
的控制器,它具有多个操作,例如GetEntityByTypeId
和GetLastEntity
i want to do a session check whenever AllEntities
controller is used 每当使用
AllEntities
控制器时,我都想进行会话检查
like suppose i request a url http://localhost:59656/AllEntities/GetLastEntity
像假设我请求一个URL
http://localhost:59656/AllEntities/GetLastEntity
then it should check weather session is assigned or not 然后应该检查是否分配了天气会话
one way of doing this would be put condition if (Session["userName"] == null)
if (Session["userName"] == null)
放置条件
in every Action of this controller 在此控制器的每个动作中
How can it be checked once for entire AllEntities
controller 如何对整个
AllEntities
控制器进行一次检查
any kind of help would be appreciated 任何形式的帮助将不胜感激
You could override the controllers Initialize
method which would fire on every action. 您可以覆盖控制器的
Initialize
方法,该方法将在每次操作时触发。 I use it to populate ViewBag elements I need on multiple/all views. 我用它来填充多个视图/所有视图上所需的ViewBag元素。
public class YourController : Controller
{
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
try
{
//do your session check here...
//alternatively, if you wanted to limit to certain actions in your controller you could say:
var actionName = requestContext.RouteData.Values["action"].ToString().ToLower();
if(actionName == "actionName" || actionName == "actionName2") {
//do your limited session work...
}
}
catch (Exception e) { }
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.