ASP. NET MVC Statistics online number function realization

Source: Internet
Author: User

Today develop a statistic to design a statistical online population. The implementation is in MVC, when a user executes an action request, inserts a user's heartbeat record into the data table, and the number of people on the line is based on that record, and the user logged in 30 minutes is online.

First design the table heartbeat record table structure model

Next, add the Checkacattribute class to inherit the ActionFilterAttribute base class and register it in the Filterconfig class.

public class Filterconfig
{
public static void Registerglobalfilters (Globalfiltercollection filters)
{
Filters. ADD (New Handleerrorattribute ());
Filters. ADD (New ASSFramework.Web.Common.CheckACAttribute ());
}
}

The Interceptor method of the Checkacattribute class is implemented as follows

  public override void onactionexecuted (ActionExecutedContext filtercontext) {
   if (Isinsertopenlog = = True) {  //Determines whether the heartbeat record is written
    //logs the action log after each action is executed to count the user's online.
    assframework.services.bll.sysopenlogbll BLL = new ASSFramework.Services.BLL.sysOpenLogBLL ();
    basecontroller Basecontroller = (basecontroller) Filtercontext.controller;
    sysopenlog model = new Sysopenlog ();
    model.userid = Basecontroller.getsysuser () .userid;       //get user ID
    MODEL.USERNAMECN = basecontroller.getsysuser () .usernamecn;   User name
    model.createddate = DateTime.Now;
    model.flag = 1;
    bll. Insert (model);
   }
   base. OnActionExecuted (Filtercontext);
  }

Business Logic Layer

public class Sysopenlogbll {

Dal.sysopenlogdal dal = new Dal.sysopenlogdal ();

<summary>

Statistics Online Number

</summary>

<returns></returns>

Public ilist

Return DAL. Getonlinellist ();

}

}

Data access layer, the use of data access to the Ibatisnet framework, the framework of light and small flexibility, the first time the use of the deep love.

public class Sysopenlogdal {
<summary>
Statistics Online Number
</summary>
<returns></returns>
Public ilistString Stmtid = "Sysopenlog.getonlinellist";
Return Assmapper.instance (). Queryforlist}

The table map XML file is as follows

<statements>

<!--statistics online, nearly half an hour of active users--

<select id= "getonlinellist" parameterclass= "map" resultclass= "Hashtable" >

Select Userid,usernamecn,max (createddate)

From Sysopenlog

GROUP BY USERID,USERNAMECN

Having MAX (createddate) >= DATEADD (Minute,-30,getdate ())

</select>

<!--Add-

<insert id= "Insert" parameterclass= "Sysopenlog" resultclass= "Int32" >

Insert into Sysopenlog (UserID, USERNAMECN, CreatedDate, flag) VALUES (#userID #, #userNameCn #, #createdDat e#, #flag #) SELECT @ @IDENTITY

</insert>

</statements>

The underlying data is written to the heartbeat record table after the action request, and the next step is to show the online population information

Add Index () to the controller

Public ActionResult Index ()
{
ASSFramework.Services.BLL.sysOpenLogBLL BLL = new Services.BLL.sysOpenLogBLL ();
Ilistreturn View (model);
}

Add Idex View Page

@{

Layout = "~/views/shared/_layout.cshtml";

}

@using System.Collections;

@using Assframework.models;

@model ilist

<!--Navigation--

@Html. Place ("home; user management; online statistics")

<div class= "Formbody" >

<div class= "Formtitle" ><span> people online </span></div>

<div class= "Toolsli" >

<ul class= "Toollist" >

@foreach (var item in Model) {

<li><a></a>

}

</ul>

</div>

</div>

The final results are as follows

This time only posted a general idea of implementation and a simple code map.

The function of statistics on-line people basically realized, but at this time to consider the heartbeat record table if the number of users, and the operation is frequent, the amount of data will be more and more, here we only need to count half an hour of user activity records, so in the SQL Server database to add a job, scheduled to delete garbage data.

ASP. NET MVC Statistics online number function realization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.