简体   繁体   English

如何在 ASP.NET Controller 中发送客户价值?

[英]How Can I send Clients value in ASP.NET Controller?

I have written a code in c#, javascript, using client library SignalR to refresh a page in database value change.我在 c#、javascript 中编写了代码,使用客户端库 SignalR 刷新数据库值更改中的页面。 My code is我的代码是

<form asp-action="Start" method="post" class="form-stacked">
    <button type="submit" id="startPractice" class="button-primary">Start Practice</button>
</form>

<script src="~/js/ignalr/dist/browser/signalr.js"></script>
<script src="~/js/chat.js"></script>

My API method is which is called while clicking start practice is我的 API 方法是在单击开始练习时调用的方法是

public async Task<IActionResult> Index(long sessionId)
{
     // Database change logic

     SignalRClientHub sr = new SignalRClientHub();
     await sr.SendMessage();

     // Rest of the logic

     return this.View();
}

 public class SignalRClientHub : Hub
    {
        public async Task SendMessage(string user = null, string message = null)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }

code of chat.js chat.js 的代码

"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/SignalRClient").build();

connection.on("ReceiveMessage", function (user, message) {
    location.reload();
});

When I click the button start practice it hits SendMessage Method, but I got an error当我单击按钮开始练习时,它会触发 SendMessage 方法,但出现错误

object reference not set to an instance

because the value of the Client was null.因为客户端的值是 null。 How Can I fix this?我怎样才能解决这个问题?

You cannot new up a hub manually.不能手动新建集线器。 In order to send to clients from outside of the hub you need to use the IHubContext<THub> .为了从集线器外部发送给客户端,您需要使用IHubContext<THub> See the docs for details https://docs.microsoft.com/aspnet/core/signalr/hubcontext?view=aspnetcore-5.0有关详细信息,请参阅文档https://docs.microsoft.com/aspnet/core/signalr/hubcontext?view=aspnetcore-5.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 如何在JavaScript中将DropdownList Selected值与JavaScript传递给Asp.net MVC 4中的Controller? - How can I pass DropdownList Selected value with JavaScript to Controller in Asp.net MVC 4? 如何将复杂的参数发送到asp.net - How can I send complex params to asp.net 无法使用js将文件值发送到asp.net mvc中的控制器 - Unable to send file value to controller in asp.net mvc with js 我想将JavaScript中的数据和文件发送到控制器(但是如何发送)(MVC ASP.NET) - I want send data and file in javascript to controller (but how)(Mvc asp.net) 如何将数据从响应发送到ASP.NET MVC中的Controller? - How to send data from react to Controller in ASP.NET MVC? 我怎样才能用js检查asp.net中“ DropDownList”的值? - how i can check value of “DropDownList” in asp.net with js? 如何将值从_layout传递到控制器asp.net - How to pass value from _layout to controller asp.net ASP.Net MVC中的简单Dropzone实现-如何在Controller中获取数据? - Simple Dropzone implementation in ASP.Net MVC - How can I get data in the Controller? 如何基于ASP.NET MVC中的控制器操作在页面javascript中设置变量? - How can I set a variable in my pages javascript based on my controller action in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM