views:

34

answers:

1

I have a custom attribute that checks conditions and redirects the user to parts of the application as is necessary per business requirements. The code below is typical:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  // ...
  if (condition) 
  {
    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
    redirectTargetDictionary.Add("action", "MyActionName");
    redirectTargetDictionary.Add("controller", "MyControllerName");
    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
  }
  // ...
  base.OnActionExecuting(filterContext);
}

I was just asked to allow the user to choose a default page that they arrive at upon logging in. Upon adding this feature, I noticed that the user can get some unusual behavior if there is no action/controller corresponding to the user's default page (i.e. if the application were modified). I'm currently using something like the code below but I'm thinking about going to explicit actions/controllers.

else if (condition)
{
  var path = "~/MyControllerName/MyActionName";
  filterContext.Result = new RedirectResult(path);
}

How do I check the validity of the result before I assign it to filterContext.Result? I want to be sure it corresponds to a working part of my application before I redirect - otherwise I won't assign it to filterContext.Result.

A: 

I don't have a finished answer, but a start would be to go to the RouteTable, get the collection, call GetRouteData with a custom implementation of HttpContextBase to get the RouteData. When done, if not null, check if the Handler is an MvcRouteHandler.

When you've got so far, check out this answer :)

Onkelborg