gpt4 book ai didi

c# - MVC 3 如何让用户查看警告/免责声明屏幕

转载 作者:行者123 更新时间:2023-11-30 20:05:05 25 4
gpt4 key购买 nike

我认为这会很简单,但我有点挣扎。我正在为使用 MVC 3 的客户开发一个项目,该项目要求用户在使用网站之前同意某些条件。我已经创建了一个标准的同意/不同意屏幕,它在第一次进入该站点时加载,但是如果用户在站点的不同部分键入地址,他们可以绕过条件,例如 www.test.com 加载条件但是如果用户键入 www.test.com/home 他们绕过了条件。

我如何才能确保他们在访问网站上的其他任何地方之前已同意这些条件?我一直在尝试一个 session 变量,我认为这是可行的方法,但是有没有一种方法可以在每个页面请求上检查这个变量,而不必在网站上的每个 Controller 操作中写入检查?

最佳答案

您可以制作自定义属性并将其添加到 Controller 的顶部。

例如:

    [AgreedToDisclaimer]
public ActionResult LoadPage()
{
return View();
}

只有在 AgreedToDisclaimer 返回 true 时才会加载 View 。

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

关于c# - MVC 3 如何让用户查看警告/免责声明屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com