0

I'm making a web api chat application with user register/login with SignalR, ASP.NET, VS Code in C#. I would like to make an online user counter, which i made in the CounterHub.cs, but I don't know, how can I use in my Welcome.cshtml page. Is it possible to sent integer data from this .CS file to .CSHTML file? Here is my CounterHub.cs file

using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;

namespace probagetrequest.Hubs
{
    public static class UseHandler
    {
        public static HashSet<string> ConnectedIds = new HashSet<string>();
    }
    public class MyHub : Hub
    {
        public override Task OnConnectedAsync()
        {
            UseHandler.ConnectedIds.Add(Context.ConnectionId);
            return base.OnConnectedAsync();
        }
        public override Task OnDisconnectedAsync(Exception exception)
        {
            UseHandler.ConnectedIds.Remove(Context.ConnectionId);
            return base.OnDisconnectedAsync(exception);
        }

        int counting = UseHandler.ConnectedIds.Count;
    }
}

I want to call the counting variable in my .cshtml file

kiszod
  • 67
  • 1
  • 9
  • What you could do is stored the variable on session and then in the controller for the view, get it and add it to the view – JamesS May 07 '20 at 11:02

1 Answers1

0

You have to follow below steps to do this.

  1. Make one public method in MyHub class which shall return count.
  2. Call this method from controller of Welcome.cshtml
  3. set this counter to viewbag/viewdata in controller and access viewbag/viewdata in the view

//Welcome controller

{
MyHub myhub=new MyHub();

viewbag.counting=muhyb.getCount(); 
// create getcount() method in MyHub class which will return count
}

Welcome.cshtml

<div> @Viewbag.counting </div>
Vishal Pawar
  • 356
  • 4
  • 12
  • thank you for your answer. I tried it, but i had 4 errors when i made the "viewbag.counting=myhub.getCount()" 1."Invalid token '=' in class, struct, or interface member declaration 2."Invalid token '(' in class, struct, or interface member declaration 3."Invalid token ';' in class, struct, or interface member declaration 3. Tuple must contain at least two elements. – kiszod May 07 '20 at 10:59
  • can you add code for new method,controller where you using the counting variable and .cshtml where you call viewbag. I can tell the solution after looking into your code – Vishal Pawar May 07 '20 at 11:02
  • public int getCount() { return UseHandler.ConnectedIds.Count; } MyHub myhub = new MyHub(); viewbag.counting=myhub.getCount(); and here is the welcome.cshtml: Online Users:
    @Viewbag.counting
    – kiszod May 07 '20 at 11:05
  • in .cshtml file i got an error, that: The name 'Viewbag' does not exist in the current context [probagetrequest] – kiszod May 07 '20 at 11:06
  • In cshtml, use capital V in view as @ViewBag.counting – Vishal Pawar May 07 '20 at 11:09
  • its good now in the cshtml, but in the .CS file I still have this 4 error. Maybe missing the ViewBag command? Do i need to use for that a specific namespace? – kiszod May 07 '20 at 11:12
  • System.Web.Mvc.dll is required. use this link get help.. https://stackoverflow.com/questions/15550899/the-name-viewbag-does-not-exist-in-the-current-context/15553331 – Vishal Pawar May 07 '20 at 11:15