Search code examples
model-view-controllercastle-monorail

Cancel controller action from Initialize()


I have come across a scenario where I have some initialization code on my conrtoller, which might identify an invalid state which will demand some user interaction.

There for, I want to redirect the customer to another page/action if that occurs. Since I don't want the initial action to run if I hit this invalid state, I want to cancel the whole request including the action.

Is this possible? We have figured out that one way to solve it is to use a Filter which reads out from Context.Items if it should cancel the action, but is there another, easier way?


Solution

  • I started reading this and immediately thought "Context and Filter" :-)

    I think that is the cleanest way to do it... That said, you could also do

    Context.UnderlyingContext.Response.Redirect("someotherurl");
    

    Which internally throws a ThreadAbortException so it skips all other code.