/// <summary> /// Carregar os devices de mobile de um arquivo em formato Yaml. /// </summary> private static void CarregarDevicesMobiles() { var assembly = typeof(UtilHelper).GetTypeInfo().Assembly; Stream resource = assembly.GetManifestResourceStream("UrlRouter.AspNetMvc.Helper.regexes.mobiles.yml"); using (var reader = new StreamReader(resource)) { var yaml = new YamlStream(); yaml.Load(reader); listaDeviceYmal = new List <DeviceMobile>(); var mapping = (YamlMappingNode)yaml.Documents[0].RootNode; foreach (var entry in mapping.Children) { string brand = ((YamlScalarNode)entry.Key).Value; var nodes = ((YamlMappingNode)entry.Value).Children; DeviceMobile device = new DeviceMobile(); device.Brand = brand; if (nodes.ContainsKey("device")) { device.Device = ((YamlScalarNode)nodes["device"]).Value; } if (nodes.ContainsKey("regex")) { device.Regex = ((YamlScalarNode)nodes["regex"]).Value; } if (nodes.ContainsKey("model")) { device.Model = ((YamlScalarNode)nodes["model"]).Value; } if (nodes.ContainsKey("models")) {//Verificar se existem models... se sim, carregar todos. var models = ((YamlSequenceNode)nodes["models"]).Children; device.Models = new List <ModelDevice>(); foreach (YamlMappingNode model in models) { ModelDevice modelDevice = new ModelDevice(); if (model.Children.ContainsKey("regex")) { modelDevice.Regex = ((YamlScalarNode)model.Children["regex"]).Value; } if (model.Children.ContainsKey("model")) { modelDevice.Model = ((YamlScalarNode)model.Children["model"]).Value; } if (model.Children.ContainsKey("device")) { modelDevice.Device = ((YamlScalarNode)model.Children["device"]).Value; } device.Models.Add(modelDevice); } } listaDeviceYmal.Add(device); } } }
/// <summary> /// Verifica se dentro do USER-AGENT existe um dispositivo móvel. /// </summary> /// <param name="userAgent">Informações do USER-AGENT do protocolo HTTP.</param> /// <returns>Existe ou não um dispositivo móvel.</returns> public static bool HasDeviceMobile(string userAgent) { bool hasDevice = false; string[] listDevicesMobile = { "feature phone", "smartphone", "tablet" }; DeviceMobile device = ObterDeviceMobile(userAgent); if (device == null) { return(hasDevice); } hasDevice = listDevicesMobile.Any(x => x.Equals(device.Device.ToLower())); return(hasDevice); }