ASP.NET 数据库更改后特定用户的 MVC 更新视图

ASP.NET MVC Update views for specific user after change in database

我正在使用 SQL 服务器在 ASP.NET MVC 5 中创建一个应用程序,其中一个模块是新闻。 新闻可以由管理员添加到主页。 用户可以评论新闻。我想知道如何实现实时向新闻添加评论,这意味着当用户正在阅读例如 ID = 5 的新闻时 有人 post 对此新闻发表评论 我想在不刷新页面的情况下获得此评论。我听说过 SignalR,但我不知道如何仅针对正在阅读特定新闻的用户调用更新,例如 ID=5.How 以便仅为该用户发送更新?不是针对所有登录用户,而是针对当前正在查看特定新闻的特定用户?

如果您决定使用 SignalR 进行实施,您可能会阅读有关 Groups:

Groups in SignalR provide a method for broadcasting messages to specified subsets of connected clients. A group can have any number of clients, and a client can be a member of any number of groups. You don't have to explicitly create groups. In effect, a group is automatically created the first time you specify its name in a call to Groups.Add, and it is deleted when you remove the last connection from membership in it.

因此,在您的情况下,当用户打开新闻时,您可以将其添加到具有指定新闻 ID 的服务器组。并且当用户对某条新闻发表评论时,您可以广播到所有连接到指定组的客户端。