Daniel Powell
Daniel Powell

Reputation: 8303

mvc implementing page that is viewed only once

I want to implement a terms and conditions page for my site that requires the user to accept the terms and conditions and probably stores that value in a cookie so I can tell if they have accepted and not continually prompt them, not all users will have accounts so I think a cookie is the way to go there.

Just trying to think of the best way to accomplish this, would it be best with a global filter that executes before any action on the site and redirects to the terms and conditions page (saving somewhere the page that was requested) if the user has not logged in, or is there a better way to do it?

Upvotes: 2

Views: 555

Answers (2)

user338195
user338195

Reputation:

In action method that will process T&C POST:

Session["AcceptedTC"] = true;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class AcceptedTermsCheck : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Check if AcceptedTC is set to true

        // If set, then do nothing

        // Otherwise redirect to TC page
    }
}

Reason for using an attribute, is that you might have some pages that won't require users to accept terms and conditions. If you were to do a check on Application_BeginRequest then you won't have that flexibility.

Upvotes: 2

Marc Armstrong
Marc Armstrong

Reputation: 266

An alternative option is to override the Application_BeginRequest method on the global.asax file.

protected void Application_BeginRequest(object sender, EventArgs e)
{
   if(ShouldIShowMessage())
   {
       this.Response.Redirect("RedirectUrl");
   }
}

private bool ShouldIShowMessage()
{
   // Decision logic here
}

EDIT Ive just thought, to clarify your decision logic would still require something like say a cookie, but it saves having to decorate controllers with an attribute or adding a global filter.

Upvotes: 1

Related Questions