FubuMVC using ModifyChainAttributes

Let’s take a look at a handy way to quickly modify a FubuMVC behavior chain by simply placing an Attribute on the action method. This technique is handy when you have a one off change you need to make to a specific action and rolling your own site wide convention is too much.

The Problem

FubuMVC uses content negotiation to control how your output model is formatted. Great stuff when you are creating endpoints to be used as an API for your fancy JavaScript framework of choice.

We ran into a problem on a specific API endpoint which supports file uploads where IE9 would barf on a JSON response after uploading a file. In this case the response was not being consumed by the client. No big deal let’s force the output to be XML to ensure that IE9 is happy.

To do this we will need to remove all non XML output formatters from the behavior chain.

Create a Modify Chain Attribute

Implementing a chain modifying attribute is easy.

  • Subclass ModifyChainAttribute.
  • Edit the parent behavior chain (via the ActionCall)
  • Done

image of code gist until my blog is fixed

You then simply apply your new custom attribute to your action. Take a look at FubuDiagnostics and see the changes to your chain. In this case there is only one output formatter XmlFormatter.

Fubu Diagnostics showing the only output formatter is for XML

More On Behaviors