采取我经常看到这样的解决方案你的例子:http://www.c-sharpcorner.com/UploadFile/91c28d/handle-session-expire-using-custom-attribute-in-Asp-Net-mvc/
,然后才能创建BaseController.cs控制器和定义OnActionExecuting方法。
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (Session["UserLogin"] == null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "ManageAccount" }, { "action", "Login" } });
}
}
}
在下一步创建HomeController.cs继承BaseController.cs文件。
public class HomeController : BaseController
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
BaseController OnActionExecuting方法处理每个请求,并检查会话控制。
[HttpPost]
public ActionResult LoggedIn()
{
Session["UserLogin"] = true;
return RedirectToAction("Index", "Home");
}
创建示例登录POST方法,发送请求集用户登陆会话参数并重定向到首页/索引页。每个你继承通话将执行每个请求的会话控制的控制器。
我希望它有帮助。
我建议你使用自定义属性。 –
@AkınAbdullahoğlu为什么不发布您以前使用过的任何示例? –
我的想法是不使用Global.asax中,这个例子会给出一个想法https://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-custom-action -filters –