MVC 5 ASP.NET Identity& SignalR

时间:2014-03-28 16:56:16

标签: asp.net signalr asp.net-identity

我有默认的Visual Studio 2013 MVC模板。我收到错误" JavaScript运行时错误:' $'未定义"在我使用ASP.NET Identity登录并导航到我创建的Match.cshtml页面之后。任何想法,为什么它没有看到jquery' $'?

[编辑] 如果我制作一个普通的HTML页面,一切都有效,所以这必须与使用布局或周围的东西有关。我使用默认模板_Layout page。

@using Microsoft.AspNet.Identity


@if (Request.IsAuthenticated)
{
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-2.1.0.min.js"></script>

<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.3.min.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 () {

        var clientID = "";
        var server = $.connection.gameServer;

        // Create a function that the hub can call to broadcast messages.
        server.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 ID from the server for our team
        server.client.responseID = function (id) {
            clientID = id;
        }

        // Start the connection.
        $.connection.hub.start().done(function () {

            server.server.requestID();

            // todo: define all the GUI events that need to send requests to the server for validation
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                server.server.send($('#displayname').val(), $('#message').val());

                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
</script>
}
else
{
    <div>Please login!</div>
}

1 个答案:

答案 0 :(得分:1)

我需要在脚本部分的Match视图中包装我的脚本。

@section scripts{
@Scripts.Render("~/Scripts/jquery.signalR-2.0.3.min.js")
@Scripts.Render("~/signalr/hubs")
etc...