views:

48

answers:

1

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?

+1  A: 

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.

Mauricio Scheffer
I'm actually doing a Response.Redirect (which is the IResponseAdapter I assume), are you saying that implicitly aborts the thread? Will that also bypass Controller.Dispose(), that would be a bad thing for me...
jishi
@jishi: IIRC Monorail's Response.Redirect merely records the redirect and sets a flag, and actually processes the redirect at the end of the pipeline, whereas the "real" HttpContext Response.Redirect throws and doesn't execute any other code.
Mauricio Scheffer
@jishi: about Controller.Dispose(), give it a try and let me know :)
Mauricio Scheffer
@jishi: there's also a RedirectToUrl(string url, bool endResponse) in IResponseAdapter that *does* call HttpContext's Response.Redirect as I described in my answer.
Mauricio Scheffer
Tried the Context.UnderlyingContext.Response.Redirect("someotherurl");and it indeed cancelled all actions (including the following initialization, but it did however invoke the .Dispose() of the current controller aswell. Using Controller.Redirect() didn't cancel the actions.
jishi