MVC4 SignalR“signalr / hubs”501未实现错误

时间:2012-06-12 15:54:32

标签: c# asp.net-mvc-4 signalr visual-studio-2012

更新 切换到IIS Express而不是Visual Studio Development Server修复了501错误。

我正在尝试让SignalR在我的MVC4应用程序中工作,但我遇到了一个奇怪的501错误。我已经按照几个不同的教程,但结果总是一样的。

任何帮助都将不胜感激。

我正在使用Visual Studio 2012 Pro RC,我通过包管理器控制台安装了SignalR 0.5.1。

我的代码:

布局

<script src="~/Scripts/jquery-1.7.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-0.5.1.js" type="text/javascript"></script>
<script type="text/javascript" src="@Url.Content("~/signalr/hubs")"></script>

查看

<script type="text/javascript">
$(function () {

    // capture the client IP
    var clientIp = $("#clientip").val();

    // create the SignalR proxy
    var userlink = $.connection.userlink;

    // create our callback for the proxy
    userlink.updateViewCount = function (message) {
        $('#view-updates').html('<p>' + message + '</p>');
    };

    // Start the connection
    $.connection.hub.start(function () {
        // send our 'record' message to the hub
        userlink.recordView(clientIp);
    });

});
</script>

<input type="hidden" id="client-ip" value="@ViewBag.ClientIpAddress" />
<div id="view-updates"></div>

Hub Class

public class UserLink : Hub
{
    public void RecordView(string IpAddress)
    {
        QContext db = new QContext();
        db.SiteVisits.Add(new SiteVisit { CreateDate = DateTime.Now, IpAddress = IpAddress });
        db.SaveChanges();

        int userCount = db.SiteVisits.Count();

        string result = string.Format("There have been {0} visits to this page.", userCount);

        Clients.updateViewCount(result);
    }
}

我收到错误

Failed to load resource: the server responded with a status of 501 (Not Implemented) http://localhost:58361/signalr/hubs
Uncaught TypeError: Cannot set property 'updateViewCount' of undefined 

3 个答案:

答案 0 :(得分:0)

如果您尚未解决此问题,我认为您可能需要这样做:

[HubName("userlink")]
public class UserLink : Hub
{
   // Implementation
}

顺便说一句,如果您的站点中有/ Signalr文件夹,那可能是导致501的原因。这会导致与使用内置Visual Studio Web服务器的~/signalr/hubs javascript引用冲突,但工作正常在IISExpress中。

答案 1 :(得分:0)

在IIS而不是Visual Studio Development Server中进行调试可以解决问题,但我不确定原因。现在我很好奇,如果有人知道为什么它会在一个而不是另一个工作。

答案 2 :(得分:0)

对于原始问题没有必要有用,但是如果使用SignalR的版本0.5.x 并安装了Visual Studio MVC工具,那么到达此处的其他人可能会发生冲突。

VS工具和SignalR之间存在已知的冲突,很快就会修复。

只需删除
Microsoft ASP.NET MVC 3 & 4 - Visual Studio 2010 Tools

Control Panel\Programs\Programs and Features