今天在ASP.NET MVC項目中,實現一個小功能,就是記錄瀏覽者客戶端的一些信息。比如瀏覽者的IP,訪問了哪些頁面,以及瀏覽器相關的信息。在資料庫中創建一張表[VisitData]: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI ...
今天在ASP.NET MVC項目中,實現一個小功能,就是記錄瀏覽者客戶端的一些信息。比如瀏覽者的IP,訪問了哪些頁面,以及瀏覽器相關的信息。
在資料庫中創建一張表[VisitData]:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[VisitData]( [VisitData_nbr] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY, [IP] [varchar](20) NOT NULL, [Controller] [varchar](30) NULL, [Action] [varchar](30) NULL, [Browser] [varchar](30) NULL, [Version] [varchar](10) NULL, [Platform] [nvarchar](30) NULL, [Languages] [nvarchar](30) NULL, [UserAgent] [nvarchar](300) NULL, [RecordDate] [datetime] NOT NULL DEFAULT (getdate()), [CreatedBy] [nvarchar](30) NOT NULL DEFAULT (suser_sname()), [UpdatedBy] [nvarchar](30) NOT NULL DEFAULT (suser_sname()), [CreateDate] [datetime] NOT NULL DEFAULT (getdate())) GOSource Code
既然有資料庫,那我們還需要寫一個存儲過程[dbo].[usp_VisitData_Insert]:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[usp_VisitData_Insert] ( @IP VARCHAR(20), @Controller VARCHAR(30), @Action VARCHAR(30), @Browser VARCHAR(30), @Version VARCHAR(10), @Languages NVARCHAR(30), @Platform NVARCHAR(30), @UserAgent NVARCHAR(300) ) AS INSERT INTO [dbo].[VisitData]([IP],[Controller],[Action],[Browser],[Version],[Languages],[Platform],[UserAgent]) VALUES (@IP,@Controller,@Action,@Browser,@Version,@Languages,@Platform,@UserAgent) GOSource Code
在資料庫方法,我們就做這些,接下來我們需要轉到ASP.NET MVC項目中,創建一個Model:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Insus.NET.Models { public class VisitData { public string IP { get; set; } public string Controller { get; set; } public string Action { get; set; } public string Browser { get; set; } public string Version { get; set; } public string Languages { get; set; } public string Platform { get; set; } public string UserAgent { get; set; } } }Source Code
創建完model之後,還需要創建一個Entity,這個實體就是數據與資料庫之相關聯:
using Insus.NET.DataBases; using Insus.NET.Models; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Insus.NET.Entities { public class VisitDataEntity { BizSP sp = new BizSP(); public void Insert(VisitData vd) { List<Parameter> param = new List<Parameter>() { new Parameter("@IP", SqlDbType.VarChar, -1, vd.IP), new Parameter("@Controller", SqlDbType.VarChar, -1, vd.Controller), new Parameter("@Action", SqlDbType.VarChar, -1, vd.Action), new Parameter("@Browser", SqlDbType.VarChar, -1,vd.Browser ), new Parameter("@Version", SqlDbType.VarChar, -1,vd.Version ), new Parameter("@Languages", SqlDbType.VarChar, -1, vd.Languages), new Parameter("@Platform", SqlDbType.VarChar, -1,vd.Platform ), new Parameter("@UserAgent", SqlDbType.NVarChar, -1, vd.UserAgent) }; sp.ConnectionString = DB.ConnectionString; sp.Parameters = param; sp.ProcedureName = "usp_VisitData_Insert"; sp.Execute(); } } }Source Code
下麵,我們考慮,只是瀏覽訪問首頁時,我們就去獲取客戶端的信息:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Insus.NET; using Insus.NET.Attributes; using Insus.NET.Models; using Insus.NET.Utilities; using Insus.NET.Entities; namespace Insus.NET.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { VisitData vd = new VisitData(); vd.IP = NetUtility.GetClientIP(); vd.Controller = this.ControllerContext.RouteData.Values["controller"].ToString(); //"Home"; vd.Action = this.ControllerContext.RouteData.Values["action"].ToString(); //"Index"; HttpBrowserCapabilities bc = System.Web.HttpContext.Current.Request.Browser; vd.Browser = bc.Browser; vd.Version = bc.Version; vd.Languages = System.Web.HttpContext.Current.Request.UserLanguages[0]; vd.Platform = bc.Platform; vd.UserAgent = System.Web.HttpContext.Current.Request.UserAgent; VisitDataEntity vie = new VisitDataEntity(); vie.Insert(vd); return View(); } } }Source Code
我們需要的數據已經拿到了。
到現在為止,還有一個問題,我們需要記錄瀏覽者訪問了哪一個頁面的記錄,我們需要把上面那段代碼粘貼至所在控制器的操作(Action)中。如果這樣多,另一個問題又來了,一個ASP.NET MVC項目這樣多。那上一個很可的事情,特別是在維護的時候,就知道它的利弊了。
寫一個靜態的Utility吧,然後每一個操作中使用這個方法名即可。嗯,想法不錯。
不過,在ASP.NET MVC中,有一個更好的方法,就是過濾(filter).
using Insus.NET.Entities; using Insus.NET.Models; using Insus.NET.Utilities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; namespace Insus.NET.Attributes { public class VisitActionFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { VisitData vd = new VisitData(); vd.IP = NetUtility.GetClientIP(); //vd.Controller = this.ControllerContext.RouteData.Values["controller"].ToString(); //"Home"; //vd.Action = this.ControllerContext.RouteData.Values["action"].ToString(); //"Index"; vd.Controller = filterContext.RouteData.Values["controller"].ToString(); vd.Action = filterContext.RouteData.Values["action"].ToString(); HttpBrowserCapabilities bc = System.Web.HttpContext.Current.Request.Browser; vd.Browser = bc.Browser; vd.Version = bc.Version; vd.Languages = System.Web.HttpContext.Current.Request.UserLanguages[0]; vd.Platform = bc.Platform; vd.UserAgent = System.Web.HttpContext.Current.Request.UserAgent; VisitDataEntity vie = new VisitDataEntity(); vie.Insert(vd); } } }Source Code