1

I am building an ASP.NET MVC3 computer support ticketing portal.

There is a maintenance state, where it is best to forbid the users from interacting with EF/Database, to avoid "collisions" I am currently getting.

I have an IMaintenanceDispatcher that has a boolean property IsOnMaintenance set to true by the business logic, whenever a background logic puts the portal in that state.

I need to redirect client requests to a parking page for the time of maintenance.

Where do I place the logic that will check if the IsOnMaintenance is true, and if so, do a redirect to a URL?

1 Answer 1

1

You could put it in an ActionFilterAttribute and apply that attribute to any applicable actions/controllers or globally.

public class IsOnMaintenanceAttribute : ActionFilterAttribute
{
    //You'll need to setup your IoC to inject this
    public IMaintenanceDispatcher InjectedMaintenanceDispatcher { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        object ticketIdObj;
        if (!filterContext.ActionParameters.TryGetValue("ticketId", out ticketIdObj))
            return;

        //Make sure it exists
        if (InjectedMaintenanceDispatcher.IsOnMaintenance(int.parse(ticketIdObj)))
        {
            var routeValues = new RouteValueDictionary(new {
                action = "parkingpage",
                controller = "maintenance",
                area = "ticket" });

            filterContext.Result = new RedirectToRouteResult(routeValues);
            return;
        }
    }
}

Note, your action method parameters needs to contain a variable named ticketId for the filterContext.ActionParameters.TryGetValue to work.

Note: I had assumed that an individual ticket is put into maintenance mode and you were wanting to check for that... but re-reading the question it seems like you want to put the whole area/site on hold. Even with that case, the ActionFilterAttribute example still holds... just not as different.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, thank you, but is there perhaps a way that would not require me to decorate every single controller with redirect functionality like this?
Will it be a global thing? If so, you can call something like GlobalFilters.Filters.Add(new IsOnMaintenanceAttribute()) inside Application_Start... that will add it globally. If you don't want it to be global, but still need it on many controllers... I would use a base class/controller.
It works. Just if I set it globally, I get the redirect loop for the Maintenance action also. So I need a way to conditionally check if the requested action isn't the one I am redirecting a client to. I guess I can check from within the filter code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.