topical media & game development

talk show tell print

#mobile-application-03-MVCDerbyService-MVCDerbyService-Results-AcceptHeaderResult.cs / cs



  using System.Web.Mvc;
  
  namespace MVCDerbyService.Results
  {
      public class AcceptHeaderResult : ActionResult
      {
          private object payload { get; set; }
  
          public AcceptHeaderResult(object data)
          {
              payload = data;
          }
  
          public override void ExecuteResult(ControllerContext context)
          {
              string accept = context.HttpContext.Request.Headers["accept"].ToLower();
  
              ActionResult result = null;
              if (accept.Contains("text/html" ))
              {
                  context.Controller.ViewData.Model = payload;
                  result = new ViewResult() { TempData = context.Controller.TempData, ViewData = context.Controller.ViewData };
              }
              else if (accept.Contains("application/json"))
              {
                  result = new JsonResult() { Data = payload, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
              }
              else if (accept.Contains("text/xml"))
              {
                  result = new XmlResult(payload);
              }
              else
              {
                  result = new HttpStatusCodeResult(406, "Type not supported. Supported MIME types are text/html, application/json, and text/xml.");
              }
              result.ExecuteResult(context);
          }
      }
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.