Signalr Hubs Not found (403)

2019-07-16 02:12发布

问题:

Right, so I've seen loads of questions about this, but none of the suggested fixes have worked for me.

I have a MVC4 project running in Visual Studio 2012/IIS Express, using SignalR to do some feedback to users. When I run the project I get a 403 error in Chrome like such:

Failed to load resource: the server responded with a status of 403 (Forbidden) http://{website}/Hubs/


Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/hubs'></script>.

Obviously I've changed the actual domain on my machine to {website}

Now, originally I had it in a folder called signalr in the root of my web project with a subfolder of hubs, but then I read on the SignalR GitHub that you shouldn't do that, so i changed it to just 'Hubs'. Still didnt fix it.

I've also seen the fix regarding putting your RouteTable.Routes.MapHubs(); in the right order. Mine looks like this:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RouteTable.Routes.MapHubs();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

I've also seen some people suggesting passing in your route as a parameter into the MapHubs() method, but that takes a string and a HubConfiguration object as a parameter so that's not working either.

My view code looks like this:

@using(Html.BeginForm("Report", "Home", FormMethod.Post, new { enctype="multipart/form-data" }))
{
    @Html.ValidationSummary(true);
    <div>
        @(InputExtensions.Hidden(Html, "submittingUserId", ViewBag.CurrentUser))
        @(InputExtensions.Hidden(Html, "signalConnectionId"))
        <br />
        @Html.EditorFor(model => model.UploadedFile)
        @Html.ValidationMessageFor(model => model.UploadedFile)
    </div>
    <div>
        <input id="subButton" type="submit" value="UploadFile" title="Upload File" />
    </div>
}

@section Scripts {
    @Scripts.Render("~/Scripts/jquery.signalR-1.1.1.min.js")
    @Scripts.Render("~/Hubs")

    <script type="text/javascript">
        $(document).ready(function () {
            var accountMovementMoveHub = $.connection.accountMovementHub;
            $("#signalConnectionId").val($.connection.connectionId);

            $.connection.hub.updateProgress = function (message) {
                $("#progress-list").append('<li>' + message + '</li>');

            };

            $.connection.hub.start();
        });
    </script>

}

I've also added these elements to the web.config:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
    </modules>
</system.webServer>  

It's been driving me absolutely insane, I've tried absolutely every suggestion I could find on here, github, and anywhere else for that matter :(

Any help would be greatly appreciated!

Thanks

ANSWER

So it turns out, I assumed that the script reference to /signalr/hubs was to the PHYSICAL folder containing the hubs. Which it isn't; it is a reference to a virtual directory, which is why if you ALSO have a physical folder called SignalR it gets confused. I moved my hubs folder to my business project, and set the script reference back to "/signalr/hubs" and it all started working.

Sorry about the stupid question and thanks for your answers.

回答1:

RouteTable.Routes.MapHubs(); maps your hubs to http://{website}/signalr/hubs

Why are you trying to get the hubs from http://{website}/Hubs? Simply access both paths from your browsers, you need to get a http response containing a lot of javascript code. If that does not work, then it means your application is not configured right.

Follow this tutorial, you can see the order of RouteTable.Routes.MapHubs() is different than what your have



回答2:

Following the inspiration above I updated my webconfig to look like this:

script src='<%: ResolveClientUrl("~/signalr/hubs") %>'

instead of

script src='<%: ResolveClientUrl("~/hubs") %>' type="text/javascript"

Not sure why, as the folder structure is just ~/Hubs relative to the page, but it works so... ZANG!!!!