I do not see the best alternatives to custom model binding. I will post my insert here if anyone else sees this. Using model binding allows the Accept header to be directly attached to the direct input of the action, allowing direct testing of return types and without forcing you to artificially have more actions than you need and not lead to dynamic typed data / bag types.
Here's a Model Binder with a supporting enum type:
public enum RequestAcceptType { NotSpecified, Json, Xml } public class RequestAcceptTypeModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } RequestAcceptType acceptType = RequestAcceptType.NotSpecified;
Here is the corresponding bit in Global.asax in the Application_Start method:
ModelBinders.Binders[typeof(RequestAcceptType)] = new RequestAcceptTypeModelBinder();
Then, to use it in your actions, simply enter an argument (any name) with an enumeration type:
public ActionResult Index(RequestAcceptType acceptType)
If no one answers the best method in a couple of days, I will take this as an answer.
source share