ASP.NET Core 5.0 - Preferred UserAgents

This article will describe how to capture a user agent string and store it with a preferred property. I will assume you have downloaded the ASP.NET Core 5.0 - Homegrown Analytics Project or created a new ASP.NET Core 5.0 Razor Pages project. See Tutorial: Get started with Razor Pages in ASP.NET Core.

Homegrown Analytics Project and Article Series

The project is feature complete and will increment the version with updates. The details page includes the version change log. This project is designed and organized to allow easy integration with existing projects. The KenHaggerty. Com. SingleUser NuGet package segregates the user logic. SQL scripts to create the schema, tables, and default data are included in the package. Details, screenshots, and related articles can be found at ASP.NET Core 5.0 - Homegrown Analytics Project.

I have upgraded the PageHit entity's UserAgent property from a string to an int. The UserAgentId relates to a child UserAgent entity with a Preferred property.

Entities > UserAgent.cs:
public class UserAgent
{
    [Key]
    public int Id { get; set; }
    [Required]
    [Display(Name = "UserAgent")]
    [StringLength(512)]
    public string UAString { get; set; }
    public bool Preferred { get; set; }
    public DateTimeOffset CreateDate { get; set; }
}

The AnalyticsMiddleware and AnalyticsServices creates a new UserAgent if it does not exist. The initial migration and CreateAnalyticsSchema SQL scripts create a default UserAgent with the primary Id property set to 1. If the AnalyticsMiddleware encounters an issue, the default UserAgent is employed.

I developed a UserAgents page which displays the UserAgent's PageHits count and provides the functions to add or remove the preferred status.

Admin UserAgent Listing Page.
Admin UserAgent Listing Page Mobile.

Rather than trying to detect robot page hits, I can filter on Preferred UserAgents.

Admin PageHit Listing Page.
Admin PageHit Listing Page Mobile.
Ken Haggerty
Created 01/02/21
Updated 01/06/21 06:02 GMT

Log In or Reset Quota to read more.

Article Tags:

Analytics EF Core Model
Successfully completed. Thank you for contributing.
Contribute to enjoy content without advertisments.
Something went wrong. Please try again.

Comments(0)

Loading...
Loading...

Not accepting new comments.

Submit your comment. Comments are moderated.

User Image.
DisplayedName - Member Since ?