SignalR不是网站工作,但在Web应用程序的工作原理 [英] SignalR not working in website but works in web application

查看:172
本文介绍了SignalR不是网站工作,但在Web应用程序的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个简单的<一个href=\"http://www.asp.net/signalr/overview/signalr-1x/getting-started/tutorial-getting-started-with-signalr\"相对=nofollow>教程和管理,以创建signalR测试Web应用程序。但是,当我尝试使用ASP.NET网站创建它,然后浏览html页面,我得到了以下错误:

 类型错误:$■连接是未定义
VAR聊天= $ .connection.chatHub;

这是我的项目结构,如果这事项:

根据我的发现, runAllManagedModulesForAllRequests 在web.config中设置为true,

是必要的,所以我已经做到了。我也跟着教程有点过时,因为我使用VS 2010(即.NET框架4)这是唯一与SignalR V兼容1.1.3。

为什么我不能在网站上得到这个工作,但在Web应用程序完美的作品?

更新:

一个解决方案(我认为是正确的)建议


  

把我的code隐藏文件在一个单独的cs文件,并将该文件CS中
  APP_ code文件夹


于是,我就改变我的html文件到一个.aspx文件。这样,我身后有一个文件code(即.aspx.cs),但我在这是什么意思移动code后面的文件,因为我嵌套.aspx文件到文件.aspx.cs困惑居住在APP_ code文件夹中是不允许的。

这是什么上述手段,所列的答案?

更新:

下面是我HTMLPage.htm脚本引用连同主要功能。

 &LT;! - 引用jQuery库。 - &GT;
&LT;脚本的src =// ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js\"></script>
&LT;! - 参考了SignalR库。 - &GT;
&LT;脚本的src =/脚本/ jquery.signalR-1.1.3.js&GT;&LT; / SCRIPT&GT;
&LT;! - 参考自动生成的SignalR枢纽脚本。 - &GT;
&LT;脚本的src =/ signalr /集线器&GT;&LT; / SCRIPT&GT;
&LT;! - 加入脚本来更新页面和发送消息.--&GT;
&LT;脚本类型=文/ JavaScript的&GT;
    $(函数(){
        //声明一个代理来引用枢纽。
        VAR聊天= $ .connection.chatHub;
        //创建集线器可以打电话到广播消息的功能。
        chat.client.broadcastMessage =功能(姓名,消息){
            // EN的Html code显示名称和消息。
            变种EN codedName = $('&LT; D​​IV /&GT;')。文本(名称)。html的();
            变种EN codedMsg = $('&LT; D​​IV /&GT;')。文本(消息)的.html();
            //信息添加到页面。
            $('#讨论')追加('&LT;立GT;&LT;强&GT;'+ EN codedName
                +'&LT; / STRONG&GT;:&安培; NBSP;&安培; NBSP; EN + codedMsg +'&LT; /李&GT;');
        };
        //获取用户名,并将其存储到prePEND邮件。
        $('#显示名称)VAL(提示(请输入您的名字:',''));
        //设置初始焦点消息输入框。
        $('#消息)专注()。
        //开始连接。
        $ .connection.hub.start()。完成(功能(){
            $('#SendMessage函数')。点击(函数(){
                //调用集线器上的发送方法。
                chat.server.send($('#显示名称)VAL(),$('#消息)VAL());
                //清除文本框和重置重点下一评论。
                $('#消息)VAL('')专注()。
            });
        });
    });
&LT; / SCRIPT&GT;


解决方案

您应经常检查控制台当事情不工作 - 在你的情况下,页面根本无法找到的SignalR脚本和引用/ signalr /集线器(和它是这么说的控制台)。如果更改URL以/WebSite18/Scripts/jquery.signalR-1.1.3.js和/ Website18 / signalr /中心,它会工作。

I followed this simple tutorial and managed to create a test web application with signalR. But when I tried to recreate it using ASP.NET website and then browse for the html page, I got the following error:

TypeError: $.connection is undefined
var chat = $.connection.chatHub;

This is the structure of my project if this matters:

Based on what I found, setting runAllManagedModulesForAllRequests to true in web.config is necessary, so I already did it. Also the tutorial I followed is a little bit outdated since I use VS 2010(i.e. .NET Framework 4) which is only compatible with SignalR v 1.1.3.

How come I can't get this working in a website but works perfectly in a web application?

Update:

One solution(which I believe to be right) suggests to

Put my code behind file in a seperate .cs file and put that cs file in App_Code folder

So I tried to change my html file into a .aspx file. This way I have a code behind file (i.e. .aspx.cs) But I'm confused on what it is meant to move the code behind file because nesting my .aspx file to a .aspx.cs file residing in App_Code folder is not allowed.

What does that quoted answer above means?

Update:

Here are my script references in HTMLPage.htm together with the main function.

<!--Reference the jQuery library. -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!--Reference the SignalR library. -->
<script src="/Scripts/jquery.signalR-1.1.3.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<!--Add script to update the page and send messages.--> 
<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub. 
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message. 
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page. 
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.  
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub. 
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment. 
                $('#message').val('').focus();
            });
        });
    });
</script>

解决方案

You should always check the console when something doesn't work - in your case, the page simply can't find the references to the SignalR script and to /signalr/hubs (and it says so in the console). If you change the urls to "/WebSite18/Scripts/jquery.signalR-1.1.3.js" and "/Website18/signalr/hubs", it will work.

这篇关于SignalR不是网站工作,但在Web应用程序的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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