views:

109

answers:

3

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

+1  A: 

I suggest you do this with an action atrribute

Marko
A: 

Creating your custom ActionFilter is the best solution. You could download ASP.NET MVC source and look at System.Web.Mvc.AuthorizeAttribute class. I think it is a good starting point for you.

eu-ge-ne
+1  A: 

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
Sam Wessel
RedirectToAction is not accessible in the CreatePaymentRequiredViewResult method.
Prasad
Hmm yes I may have over-simplified, when I modified our production code for that example. Updating my example...
Sam Wessel