How to get user Browser name ( user-agent ) in Asp.net Core?

后端 未结 5 1957
攒了一身酷
攒了一身酷 2020-12-02 11:49

Can you please let me know how to get the browser\'s name that the client is using in MVC 6, ASP.NET 5?

5条回答
  •  心在旅途
    2020-12-02 12:08

    I have developed a library to extend ASP.NET Core to support web client browser information detection at Wangkanai.Detection This should let you identity the browser name.

    namespace Wangkanai.Detection
    {
        /// 
        /// Provides the APIs for query client access device.
        /// 
        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().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());
            }
        }
    }
    

提交回复
热议问题