Exemplo n.º 1
0
 public static string GetFormat(FormatCollection formatCollection, string[] acceptTypes)
 {
     foreach (var mimeType in MimeTypes.Parse(acceptTypes))
     {
         foreach (var key in formatCollection.Keys)
         {
             if (mimeType.Format == key)
             {
                 return(key);
             }
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        public static ActionResult GetResult(FormatCollection formatCollection, RouteValueDictionary routeValues, string[] acceptTypes)
        {
            var format = "html"; // default to html if no format extension is specified

            if (routeValues["format"] == null)
            {
                if (acceptTypes.Any())
                {
                    format = GetFormat(formatCollection, acceptTypes);
                    if (string.IsNullOrEmpty(format))
                    {
                        if (formatCollection.Default != null)
                        {
                            return(formatCollection.Default);
                        }
                        if (formatCollection["html"] != null && acceptTypes.Length == 1 && acceptTypes.First() == "*/*")
                        {
                            return(formatCollection["html"].Invoke());
                        }
                        return(new HttpStatusCodeResult(406));
                    }
                }
                else if (!formatCollection.Any())
                {
                    return(new HttpStatusCodeResult(406));
                }
            }
            else
            {
                format = routeValues["format"].ToString();
            }

            if (!formatCollection.ContainsKey(format))
            {
                if (formatCollection.Default != null)
                {
                    return(formatCollection.Default);
                }
                return(new HttpNotFoundResult());
            }

            return(formatCollection[format].Invoke());
        }