Redirect to Page when not paid

asp.net-mvcredirect

I am using MVC with C#. I need to take the user to a payment page if the user didn't pay for an item. I need to have a common class to check this functionality and redirect to payment page.

Like inheriting all the controllers to a Base Controller. In that base controller i have to check this payment status for some controller and actions(i.e. ViewPage) and redirect to payment page.

Please someone suggest the best way to do this

Best Answer

Create a custom actionFilterAttribute like so (this example works from having your item stored in the session, but you could modify this as required):

public abstract class RequiresPaymentAttribute : ActionFilterAttribute
{
    protected bool ItemHasBeenPaidFor(Item item)
    {
        // insert your check here
    }

    private ActionExecutingContext actionContext;

    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {
        this.actionContext = actionContext;

        if (ItemHasBeenPaidFor(GetItemFromSession()))
        {
            // Carry on with the request
            base.OnActionExecuting(actionContext);
        }            
        else
        {
            // Redirect to a payment required action
            actionContext.Result = CreatePaymentRequiredViewResult();
            actionContext.HttpContext.Response.Clear();
        }
    }

    private User GetItemFromSession()
    {
        return (Item)actionContext.HttpContext.Session["ItemSessionKey"];
    }

    private ActionResult CreatePaymentRequiredViewResult()
    {
        return new MyController().RedirectToAction("Required", "Payment");
    }
}

Then you can simply add an attribute to all controller actions that require this check:

public class MyController: Controller
{
    public RedirectToRouteResult RedirectToAction(string action, string controller)
    {
        return RedirectToAction(action, controller);
    }

    [RequiresPayment]
    public ActionResult Index()
    {
        // etc