如何在mvc3视图中显示“登录用户的计数” [英] How to display "Count of the Login users in mvc3 view

查看:67
本文介绍了如何在mvc3视图中显示“登录用户的计数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我必须在登录后显示主页中登录用户的数量,应该为所有用户显示。总登录用户是: 1/2 / ..基于用户数量。



我使用了Application [UserCount]变量并在不同浏览器中测试。

当我登录时,显示计数完全正确,但是当我注销已经在应用程序变量中维护时。





例如:



登录



登录用户1



在另一个浏览器中

登录



登录用户2



在另一个浏览器中

登录



登录用户3



退出后退出最后一个



但应用程序维护之前的价值并显示日志记录用户4





因此我需要在退出时减少日志记录用户



这是我的代码:

Hi,

I have to display the count of logging users in home page after login, that should be display for all users.like Total login users are: 1 / 2/.. based on users count.

I have used Application["UserCount"] variable and test in different browsers.
when i login that has display count perfectly but when i logout that have been maintaining in Application variable.


Example:

Login

logging users 1

in another browser
login

logging users 2

in another browser
login

logging users 3

after that logout final one

but the application is maintaing previous value and display logging users 4


so i need to decrease the logging users when i logout

Here is my code:

public static int count = 0;  //for count of visitors

public void Application_Start(Object sender, EventArgs e)
{
    Application["myCount"] = count;
    AreaRegistration.RegisterAllAreas();
        
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}

public void Application_End(Object sender, EventArgs e)
{
    Application["myCount"] = Application["myCount"];
}

public void Session_Start(Object sender, EventArgs e)
{
    count = Convert.ToInt32(Application["myCount"]);
    Application["myCount"] = count + 1;
}

public void Session_End(Object sender, EventArgs e)
{
    if (Session["PNetUserName"]!=null)
    {
        Session.Abandon();
        Session.Clear();
        Application["myCount"] = count - 1;
        count = Convert.ToInt32(Application["myCount"]);
    }
}





请帮助解决此问题。



谢谢。



Please help how to resolve this.

Thank you.

推荐答案

你可以通过将它作为属性来简化你的计数变量的设置,我相信这也可以解决你的问题。



You can simplfy the setting of your count variable by using it as a property, which I believe will also solve your problem.

public static int Count
{
    get
    { 
        return Application["myCount"] != null
                       ? Convert.ToInt32(Application["myCount"] 
                       : 0;
    }
    set { Application["myCount"] = value; }
}





然后只需使用短手算子递增和递减计数器:



Then simply use the short hand operators for incrementing and decrementing the counter:

public void Session_Start(Object sender, EventArgs e)
{
    Count++;
}

public void Session_End(Object sender, EventArgs e)
{
    if (Session["PNetUserName"]!=null)
    {
        Session.Abandon();
        Session.Clear();
        Count--;
    }
}


这篇关于如何在mvc3视图中显示“登录用户的计数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆