您可以创建自定义属性并将其添加到控制器的顶部。
例如:
[AgreedToDisclaimer]
public ActionResult LoadPage()
{
return View();
}
如果AgreedToDisclaimer返回true,因为这只会加载视图。
public class AgreedToDisclaimerAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");
// logic to check if they have agreed to disclaimer (cookie, session, database)
return true;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
// Returns HTTP 401 by default - see HttpUnauthorizedResult.cs.
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "ActionName" },
{ "controller", "ControllerName" },
{ "parameterName", "parameterValue" }
});
}
}
http://msdn.microsoft.com/en-us/library/dd410209(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.handleunauthorizedrequest.aspx
谢谢达伦,应该做我以后的事情,虽然如果他们没有查看免责声明,那么我需要将他们重定向到免责声明。根据您的建议,我所做的只是将它们重定向到登录屏幕,是否可以重定向到另一个URL? – 2012-08-15 11:36:59
是的,你可以使用HandleAuthorisedRequest重定向到免责声明。我会更新我的答案。 – 2012-08-15 11:48:31
现货Darren正是我需要感谢的帮助! – 2012-08-15 12:33:48