简体   繁体   中英

ASP.NET mvc signalr script not running

So I have implemented a simple signalR chat module within my ASP.NET mvc application. But the JavaScript on my chat page isn't being executed. I have the latest jquery libraries which I needed to install through NuGet for SignalR but it still doesn't work.

Here is how I load my javascripts:

<head>
<meta charset="utf-8" />
<title>Studiemeter Tech Support</title>
@Styles.Render("~/Content/Site.css")
@Styles.Render("~/Content/bootstrap.min.css")
@Styles.Render("~/Content/sb-admin-2.min.css")
@Styles.Render("~/Scripts/vendor/metisMenu/metisMenu.min.css")
@Styles.Render("~/Scripts/vendor/morrisjs/morris.css")
@Styles.Render("~/Scripts/vendor/font-awesome/css/font-awesome.min.css")
@Styles.Render("~/Content/login.css")
@Scripts.Render("~/Scripts/jquery-1.12.4.min.js")
@Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")
@Scripts.Render("~/Scripts/vendor/morrisjs/morris.min.js")
@Scripts.Render("~/Scripts/vendor/bootstrap/js/bootstrap.min.js")
@Scripts.Render("~/Scripts/vendor/metisMenu/metisMenu.min.js")
@Scripts.Render("~/Scripts/vendor/raphael/raphael.min.js")
@Scripts.Render("~/Scripts/dist/js/sb-admin-2.js")
@Scripts.Render("~/Scripts/modernizr-2.6.2.js")
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Studiemeter Ticket Systeem</title>

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

Here is my chat page:

SignaRChat.cshtml

<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="discussion"></ul>
</div>
@section scripts {
    <script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>
    <script src="~/signalr/Hubs"></script>
    <script>
        console.log("ok lol");
        $(function () {
            console.log("ok cuck");
            var chat = $.connection.chathub;
            chat.client.addNewMessageToPage = function (name, message) {
                $('#discussion').append('<li><strong>' + htmlEncode(name)
                + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            $('#displayname').val(prompt('Enter your name:', ''));
            $('#message').focus();
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                chat.server.send($('#displayname').val(), $('#message').val());
                $('#message').val('').focus();
            });
        });
    });

    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    }
</script>
}

I also have created the ChatHub class and the Startup script, as you can see below:

ChatHub.cs

namespace TicketSystem.Hubs
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the addNewMessageToPage method to update clients.
            Clients.All.addNewMessageToPage(name, message);
        }
    }
}

Startup.cs

[assembly: OwinStartup(typeof(TicketSystem.Hubs.Startup))]
namespace TicketSystem.Hubs
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

When I go to the chat page there's no dialog and the javascript doesn't work. I quite new to this and I don't know what I'm doing wrong here. Can someone please tell me what's going wrong here?

I got it. This problem was caused by the use of sections an layouts. I just discovered that you had to put the following order:

RenderBody()
RenderSection("scripts", required: false)

in this order:

RenderSection("scripts", required: false)
RenderBody()

My chat function still gives some errors regarding the functions I'm using, but this I can handle. I'm just new to ASP.NET MVC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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