[英]How to check sessions on every action of a controller?
我有一個名為AllEntities
的控制器,它具有多個操作,例如GetEntityByTypeId
和GetLastEntity
每當使用AllEntities
控制器時,我都想進行會話檢查
像假設我請求一個URL http://localhost:59656/AllEntities/GetLastEntity
然后應該檢查是否分配了天氣會話
if (Session["userName"] == null)
放置條件
在此控制器的每個動作中
如何對整個AllEntities
控制器進行一次檢查
任何形式的幫助將不勝感激
您可以覆蓋控制器的Initialize
方法,該方法將在每次操作時觸發。 我用它來填充多個視圖/所有視圖上所需的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.