将 SignalR 与 ASP.NET MVC3 结合使用
我正在尝试在应用程序中使用 SignalR 集线器,但目前运气不佳。我已通读 Scott Hanselman 的文章,还查看了 这篇 博客文章展示了如何在 MVC 中实现它,基本上做同样的事情。不幸的是,我没有运气。在客户端,JavaScript 似乎工作正常[除了没有发生任何事情之外],但是当我在代码中放置断点时,它显示正在调用控制器,而不是集线器代码。我使用的代码是这样的:
// Client side javascript:
var hooking;
$(function() {
// Setup SignalR
hooking = $.connection.hooking;
hooking.removeLead = function(ref) {
$("lead" + ref).remove();
};
$.connection.hub.start();
}
});
// Hooking.cs (placed in application root)
public class Hooking : Hub
{
public void Submit(string jsonString)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Deserialize<HookingLeadResult>(jsonString);
Clients.removeLead(json.Ref); // Remove lead from client hooking windows
// update lead gen
}
}
当我稍后在代码中调用 hooking.submit(resultJson);
时,由于某种原因,它会调用我的 HookingController (这是当前页面)的 Index 操作。有人知道如何正确调用 Hooking.cs 中的 Submit 函数吗?
I'm attempting to use SignalR hubs in an application, but am having no luck currently. I've read through Scott Hanselman's article, and also looked at this blog post which shows how to implement it in MVC, basically doing the same thing. Unfortunately, I'm having no luck. On the client side, the javascript seems to work fine [apart from nothing happening] but when I place breakpoints in the code it shows that the controller is being called, not the hub code. The code I'm using is this:
// Client side javascript:
var hooking;
$(function() {
// Setup SignalR
hooking = $.connection.hooking;
hooking.removeLead = function(ref) {
$("lead" + ref).remove();
};
$.connection.hub.start();
}
});
// Hooking.cs (placed in application root)
public class Hooking : Hub
{
public void Submit(string jsonString)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Deserialize<HookingLeadResult>(jsonString);
Clients.removeLead(json.Ref); // Remove lead from client hooking windows
// update lead gen
}
}
When I call hooking.submit(resultJson);
later in my code for some reason it calls the Index action of my HookingController (which is the current page). Anyone know how to correctly call the Submit function from Hooking.cs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否查看了 firebug 或其他嗅探 http 流量的工具中的网络流量以确保没有错误?可能和路由有关系。
另请注意,您不需要序列化我们为您所做的任何事情。只需来回发送对象即可。
当您在 mvc 应用程序中时,您需要像任何其他静态脚本一样包含 hub 脚本:
Did you look at the network traffic in firebug or some other tool that sniffs http traffic to make sure you have no errors? It might be something to do with routing.
Also a side note, you don't need to serialize anything we do that for you. Just send objects back and forth.
When you're in an mvc app you need to include the hub script like any other static script:
这有两个问题 - 1.由于某种原因,旧版本的 jquery.validate 导致了错误,但更新它解决了这个问题。 2. 我尝试转换为可空 int,我猜这是不受支持的。我确信有更优雅的方法来解决这个问题,但我只是将可为空的数据输入转换为字符串并解析它们(如果不为空)
There were two issues with this - 1. For some reason an old version of jquery.validate was causing errors, but updating it sorted this. 2. I attempted to cast to a nullable int, which I guess isn't supported. I'm sure there are more elegant ways to solve this, but I simply cast my nullable data inputs to strings and parsed them if not null
jquery.validate.js 是罪魁祸首。由于某种原因它触发了 GET 请求,在排除 js 文件后,SignalR 请求开始触发 POST 请求。也许我应该像 Jordan Wallwork 提到的那样更新它。
jquery.validate.js was the culprit. For some reason it was triggering GET request, and after excluding the js file, SignalR requests started triggering POST request. Perhaps I should update it as Jordan Wallwork mentioned.