简体   繁体   中英

MVC4 SignalR “signalr/hubs” 501 Not Implemented Error

Update: Switching to IIS Express instead of the Visual Studio Development Server fixed the 501 error.

I'm trying to get SignalR working in my MVC4 application but I am running into a strange 501 error. I have followed a couple different tutorials but the result is always the same.

Any help would be appreciated.

I am using Visual Studio 2012 Pro RC and I installed SignalR 0.5.1 through the package manager console.

My code:

Layout

<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>

View

<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);
    }
}

Error I am receiving

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 

If you haven't fixed this yet, I think that you might need to do this:

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

As an aside, if you have a /Signalr folder in your site, that is probably what caused the 501. That causes conflicts with the ~/signalr/hubs javascript reference using the built in Visual Studio web server, but works fine in IISExpress.

Debugging in IIS instead of the Visual Studio Development Server fixes the issue, but I'm not sure why. Now I'm curious if anyone knows why it would work in one and not the other.

Not necessary useful for the original question, but others arriving here, might have a conflict if working with version 0.5.x of SignalR and have the Visual Studio MVC tools installed.

There's a known conflict between the VS tools and SignalR that will be fixed sometime soon.

Simply remove the
Microsoft ASP.NET MVC 3 & 4 - Visual Studio 2010 Tools
from
Control Panel\\Programs\\Programs and Features

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