FubuMVC Hijacking Behaviors

Have you every wanted a convention to wrap an endpoint and conditionally transfer the original chain to a different one? No server redirect here just plain old hijack your currently scheduled response with another one. This is the FubuMVC equivalent of the classic ASP Server.Transfer. Here are a couple of examples on how to do this.

Scenario: Web Service API Exception Handling

We have a web services application where when things go wrong we want a pretty error page for the developer type who is consuming the service. You don’t want an ugly yellow and red YSOD you have too much style for that.

Answer: Wrap the Behavior Chain

In FubuMVC you can easily wrap one behavior with another.

This simple convention pulled from Dovetail Bootstrap wraps all of our API actions with the exception handling behavior. The generic argument is the type of the request you wish to redirect to when something goes wrong.

The behavior does the heavy lifting putting a try/catch around a wrapped behavior. When an exception is thrown and Asp.Net custom errors are enabled we need to hijack the behavior and return a different result. The generic argument type is used to build the target chain’s input model and invoke that behavior chain using a FubuPartial.

A Partial? What the fsck is a FubuPartial?

A FubuPartial is a really silly way of saying that we want to render the result of an input model. A partial can be rendered two ways: with or without authorization nodes. Invoke() checks the authorization nodes and InvokePartial() does not. Why wouldn’t you want to authorize? Well your behavior is likely already positioned after the authorization nodes  in the behavior chain so it might not benefit anyone to do yet another authorization. Unless that is you do for this particular part of your application.

In the end a FubuPartial is a handy way to get and render the contents of any behavior chain given you have the input model. Tldr; Ok there you go this was the big take away from this post. The rest is window dressing and example code and chest thumping.

Scenario: Single Page Web App

Often with a single page application using a client side framework like Backbone.js you want to support deep linking using  browser History and pushState. Let’s support transferring web service requests coming from search engines and user’s bookmarks to the site’s actual home page while leaving the URL intact so the client side framework can be in-charge of rendering the page.


A single page app does not reload it uses AJAX requests for everything. Your single page FubuMVC application will likely have RESTful-ish web services returning JSON for each of the views presented to the user. Urls like:

  • /customer/new
  • /customer/1
  • /customer/1/edit

Your web services only return JSON and likely don’t have HTML versions of their results. If a user bookmarks one of these links they will only get back JSON. Yuck!

Enter TransferAPIRequestsTo Behavior

How about we Hijack web service requests that come from actual browsers. We do this in similar fashion as the first example:

The API requests are wrapped with a behavior that checks to see if the request’s mimetype want’s HTML. If so, just like before we get the partial for the generic argument’s input model and render it.


A couple of interesting scenarios that leverage the flexibility of FubuMVC in similar yet very different ways.