How to determine the browser in Asp.Net Core 2 and determine if it is a mobile device.
.Net Framework, alt
First of all, there is no query on the server in Asp.Net Core as in Asp.Net Framework.
In asp.net MVC .Net framework and Webforms you could still examine the client browser with
System.Web.HttpContext.Current.Request.Browser.IsMobileDevice |
Therefore you can evaluate the capabilities of the web client with 3 possibilities.
With the Nuget packages: 51Degrees paid, with Wangkanai or you can simply read the Request.Headers.UserAgent and evaluate it yourself.
Here is the browser detection with Wangkanai
To recognize the browser with Wangkanai, you have to install the Nuget Package Wangkanai.Detection.Browser.
Under the Nuget Package Manager you can find several entries for Wangkanai and can install the Wangkanai.Detection.Browser here.
The Nuget Package Detection Browser must be installed as Dependencies-> Nuget as Wangkanai.Detection.Browser.
Then you have to in the file aspnetcore-> startup.cs in the area ConfigureServices ()
Insert the lines services.AddDetectionCore () and .AddBrowser.
// Add detection services. services.AddDetectionCore() .AddBrowser();
|
Result from:
((Wangkanai.Detection.Collections.Safari) test) ._agent
mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/66.0.3359.117 safari/537.36 |
Unfortunately, the result is wrong, as you can see, because the browser is really a Chrome browser
clientBrowser.Type = "Safari" is wrong.
The Asp.Net Core 2 Code was tested with Visual Studio and as a test browser: Chrome on Windows 10
Asp.Net Core 2: Controller-> Initialization
Integration of Wangkanai Browser Detection
In the Using section you have to add Wangkanai.Detection.
Then expand the initialization line of the AspNetCore Controller with the, IBrowserResolver browser
And assign to the local element
private readonly IBrowserResolver _browser; |
With
_browser = browser; |
Header code of the controller
////using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Readdy.Data; using Readdy.Models; using System.Security.Claims; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using System.IO; //delete File, move using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ViewFeatures; using System; //Convert using Wangkanai.Detection;
namespace Readdy.Controllers {
public class NotesController : Controller {
#region Controller Init private readonly ApplicationDbContext _dbContext; private readonly IHostingEnvironment _hostingEnvironment; private readonly IBrowserResolver _browser; public NotesController(ApplicationDbContext dbContext, IHostingEnvironment hostingEnvironment, IBrowserResolver browser) { //----< Init: Controller >---- _dbContext = dbContext; _hostingEnvironment = hostingEnvironment; _browser = browser; //----</ Init: Controller >---- } #endregion
|
In the Controller-> Action
Browser query
At runtime you can then query the browser in the controller in the addressed Asp.Net Core Action as here ..
// GET: /Note Root public async Task<IActionResult> Index_All() { ///-------------< Index_All >------------- //--< Get User ID >-- //internal referenz-Number for tracking in tables long IDCurrent_User = await UserInfo_Methods.getIDUser_as_Number(this.User,_dbContext ) ; //--</ Get User ID >--
//< Own_Views_logger > await Counter_Logger.counter_Note_List_erhoehen(IDCurrent_User); //</ Own_Views_logger >
//--< Get Linq.Query >-- //*gets last 10 Notes with View_Sum var query = (from n in _dbContext.tbl_Notes join u in _dbContext.Users on n.IDUser equals u.IDUser join s in _dbContext.tbl_User_Sums on n.IDUser equals s.IDUser
where n.IsDraft==false orderby n.IDNote descending select new { n, u.UserName,u.has_Profil_Image,u.sumFollowers, s.intSumViews_Others }).Take(30); //--</ Get Linq.Query >--
//----< fill Data_to_View >---- List<Notes_Index_DataModel> dataList = new List<Notes_Index_DataModel>(); //---< @Loop: Rows >--- foreach (var row in query) { //--< Row to Data >-- //< correct > string sShort = row.n.Text; if (sShort.Length > 255) { sShort = sShort.Substring(0, 255); }
row.n.Text = sShort; //</ correct >
//< Data > Notes_Index_DataModel item = new Notes_Index_DataModel(); item.Note = row.n; item.Ownername = row.UserName; item.has_Profil_Image = System.Convert.ToBoolean(row.has_Profil_Image); item.sumFollowers = row.sumFollowers; item.sumViews = row.intSumViews_Others; //</ Data >
//< add > dataList.Add(item); //</ add > //--</ Row to Data >-- } //---</ @Loop: Rows >--- //----</ fill Data_to_View >----
//< data to view > Notes_Index_View_DataModel dataView = new Notes_Index_View_DataModel(); dataView.IDCurrent_User = IDCurrent_User; dataView.List_Notes_with_Owner = dataList; //</ data to view >
//< out to view >
var test = _browser.Browser; return View("index_all", dataView); //</ out to view > ///-------------</ Index_All >------------- }
|