2015-02-22 5 views

risposta

46

Penso che sia stato facile. Hai la risposta in Request.Headers["User-Agent"].ToString().

+2

questo restituito tutti i nomi browser per me – kobosh

+0

@kobosh 'Request.Headers [ "User-Agent"]. ToString()' –

+3

Attenzione questo si tradurrà se un KeyNotFoundException se la richiesta non ha alcun User-Agent! Assicurati di usare .ContainsKey prima di controllare! – user169771

1

Ho sviluppato una libreria per estendere ASP.NET Core per supportare il rilevamento delle informazioni del browser Web client a Wangkanai.Detection Ciò dovrebbe consentire di identificare il nome del browser.

namespace Wangkanai.Detection 
{ 
    /// <summary> 
    /// Provides the APIs for query client access device. 
    /// </summary> 
    public class DetectionService : IDetectionService 
    { 
     public HttpContext Context { get; } 
     public IUserAgent UserAgent { get; } 

     public DetectionService(IServiceProvider services) 
     { 
      if (services == null) throw new ArgumentNullException(nameof(services)); 

      this.Context = services.GetRequiredService<IHttpContextAccessor>().HttpContext; 
      this.UserAgent = CreateUserAgent(this.Context); 
     } 

     private IUserAgent CreateUserAgent(HttpContext context) 
     { 
      if (context == null) throw new ArgumentNullException(nameof(Context)); 

      return new UserAgent(Context.Request.Headers["User-Agent"].FirstOrDefault()); 
     } 
    } 
} 
+0

Come funziona? Vedo che hai un 'DeviceResolver.cs' per scoprire se si tratta di un dispositivo mobile, un tavolo o un desktop, ma non riesco a vedere un codice simile per estrarre i dettagli dell'intestazione dell'agente utente. – thoean

+0

Ho aggiornato il repository e il readme sta diventando più maturo. https://github.com/wangkanai/Detection –

Problemi correlati