简体   繁体   English

使用SignalR在ASP.Net中聊天应用程序中的错误

[英]Error in chat application in ASP.Net using SignalR

I am developing a web application which has a chat feature. 我正在开发一个具有聊天功能的Web应用程序。 I started developing using SignalR but I got an error while running that particular web page alone.This is the error I am getting. 我开始使用SignalR进行开发,但仅运行该特定网页时遇到错误,这就是我得到的错误。

Parser Error

Description: An error occurred during the parsing of a resource 
required to service this request.Please review the following specific 
parse error details and modify your source file appropriately. 

Parser Error Message: 'Virtual_Classroom.chat' is not allowed here 
because it does not extend class 'System.Web.UI.Page'.

Source Error: 
Line 1:  <%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="chat.aspx.cs"       Inherits="Virtual_Classroom.chat" %>
Line 2:  
Line 3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Source File: /chat.aspx    Line: 1 

Virtual Classroom is the name of my project and chat.aspx is the web page. 虚拟教室是我的项目的名称,chat.aspx是网页。 I compiled and made the chat.aspx page alone to run.The code behind file in chat.aspx is as follows. 我编译并单独运行chat.aspx页面。chat.aspx文件背后的代码如下。

namespace Virtual_Classroom
{
  [HubName("myChatHub")]
  public partial class chat : Hub
  {
    public void send(string message)
    {
      Clients.All.addMessage(message);
    }
  }
}

In order to make the class derive from the Hub class I removed System.Web.UI.Page and added Hub in the above code. 为了使该类从Hub类派生,我删除了System.Web.UI.Page并在上述代码中添加了Hub。 I know that is causing the error.But I don't know how to get rid of that error I am new to .Net . 我知道这是导致错误的原因。但是我不知道如何摆脱这个错误,我是.Net的新手。 Please help.Thanks in advance !! 请帮助。谢谢!

That is easy. 那很容易。 Your web page chat.aspx consists of two parts - the layout ( .aspx) and code behind ( .aspx.cs) which are parts of the same class, and it is required that the class in code behind should derive (extend as stated in the question) from System.Web.UI.Page . 您的网页chat.aspx由两部分组成-布局( .aspx)和后面的代码( .aspx.cs),它们是同一类的一部分,并且要求后面的代码中的类应派生(按规定进行扩展)在问题中)来自System.Web.UI.Page So this change should be reverted, as you've broken your page. 因此,当您破坏了页面时,应还原此更改。

If you want to create a signal r hub for the chat - you should create a separate class for that (in this case it is preferred that this class as well should be in a separate *.cs file) and of course give it another name - I suppose ChatHub would be suitable in your case. 如果您想为聊天创建一个信号中心,则应为此创建一个单独的类(在这种情况下,该类也应放在单独的* .cs文件中),当然还要给它起另一个名字-我想ChatHub适合您的情况。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM