views:

45

answers:

1

Idea is to use same action methods for different types of results I.E.

/category/details/?resultFormat=json

/category/details/?resultFormat=xml

So to have some kind of ActionResult helper that contains registered pairs of value resolvers

"json", JsonValueResolver

"xml", XmlResolver

etc...is there already solution for this or I have to think some kind of custom resolver? Automapper has good solution for value resolving. Any ideas?

+1  A: 
public class SmartResult : ActionResult
{
 public override void ExecuteResult(ControllerContext context)
 {
  if (context == null)
  {
   throw new ArgumentNullException("context");
  }

  if (context.HttpContext.Request.QueryString["ResultFormat] == "json")
  {
   JavaScriptSerializer serializer = new JavaScriptSerializer();
   context.HttpContext.Response.Write(serializer.Serialize(this.Data));
  } else if(context.HttpContext.Request.QueryString["ResultFormat] == "xml")
                    {
        ...serialize using xmlserializer
  }else{
   throw new InvalidOperationException();
  }
 }

 public object Data { get; set; }
}
mcintyre321