SignalR“访问XMLHttpRequest ..."ASP.NET MVC中的错误 [英] SignalR "Access to XMLHttpRequest ..." error in ASP.NET MVC

查看:51
本文介绍了SignalR“访问XMLHttpRequest ..."ASP.NET MVC中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET中使用SignalR( Microsoft.AspNet.SignalR .SystemWeb v2.4.0和 Microsoft.AspNet.SignalR.Core v2.4.1.0 )MVC应用程序,在调试时,我在访问XMLHttpRequest3A%22demohub%22%7D%5D& __ = 1569323349167"rel =" nofollow noreferrer> https://demo.com/signalr/negotiate?clientProtocol=2.1&connectionData=%5B%7B%22name%22%3A来自来源" http://localhost:20700 ">"已被CORS策略阻止:所请求的资源上没有"Access-Control-Allow-Origin"标头." 错误.通过在网上查看,我已经在Startup.cs上完成了一些配置,如下所示,但是由于我不使用ASP.NET Core(我使用ASP.NET MVC),因此无法使用此配置.任何想法?

I use SignalR (Microsoft.AspNet.SignalR.SystemWeb v2.4.0 and Microsoft.AspNet.SignalR.Core v2.4.1.0) in an ASP.NET MVC application and when debugging, I encounter "Access to XMLHttpRequest at 'https://demo.com/signalr/negotiate?clientProtocol=2.1&connectionData=%5B%7B%22name%22%3A%22demohub%22%7D%5D&_=1569323349167' from origin 'http://localhost:20700' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." error. By looking on the web, I have concluded some configurations on Startup.cs as shown below, but I cannot use this configuration as I do not user ASP.NET Core (I use ASP.NET MVC). Any idea?

public void ConfigureServices(IServiceCollection services)
{
services.addc .AddCors(options =>
{
    options.AddPolicy(_corsPolicyName,
    builder =>
    {
        builder.WithOrigins(
                "https://localhost:5001",
                "https://localhost:4001",
                "https://localhost:44307",
                "https://localhost:44394"
            )
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials();
    });
});

services.AddSignalR();
}

更新:这是我用来解决该问题的配置:

Update: Here is my configuration to fix the problem:

Startup.cs:

Startup.cs:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(Demo.Web.UI.App_Start.Startup))]
namespace Demo.Web.UI.App_Start
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

web.config:

web.config:

<system.webServer>

    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="http://localhost:20700"/>
            <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS"/>
            <add name="Access-Control-Allow-Headers" value="Content-Type"/>
        </customHeaders>
    </httpProtocol>    

</system.webServer>

推荐答案

我设法使其正常运行,我已将源代码发布在GitHub

I've managed to make it work, i've published the source on on GitHub https://github.com/jonathanlarouche/stackoverflow_q58027442

先决条件软件包是:具有以下Nuget软件包的Asp.Net Web应用程序:
Microsoft.AspNet.SignalR.Core 2.4.1
Microsoft.AspNet.SignalR.JS 2.4.1
Microsoft.AspNet.SignalR.SystemWeb 2.4.0
Microsoft.Owin.Security 2.1.0
Microsoft.Owin 4.0.0

Prerequisites packages are : Asp.Net Web Application with the following Nuget packages :
Microsoft.AspNet.SignalR.Core 2.4.1
Microsoft.AspNet.SignalR.JS 2.4.1
Microsoft.AspNet.SignalR.SystemWeb 2.4.0
Microsoft.Owin.Security 2.1.0
Microsoft.Owin 4.0.0

web.config < system.webServer> 节点.
重要访问控制权限凭据 在使用 Allow-Origin

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:20700" />
        <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Credentials" value="true"/>
      </customHeaders>
    </httpProtocol>
</system.webServer>

Startup.cs 配置文件

public class Startup
{

    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

在Git解决方案中,我已将我的应用程序配置为在端口 49611 下运行,然后将Html页面发布到在端口 20700 下运行的空网站.现在,SignalR请求通过端口49611传递,并且CORS正常工作.

In the Git solution, I've configured my Application to run under port 49611, then i've published the Html page to a empty website running under port 20700. SignalR requests now pass on port 49611 and CORS are working.

SignalR客户端HTML :(从 http://localhost:20700/ http://localhost:49611/)

SignalR Client Html: (Working when running from http://localhost:20700/ or http://localhost:49611/)

<script type="text/javascript" src="/Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.signalR-2.4.1.min.js"></script>
<script type="text/javascript" src="http://localhost:49611/SignalR/hubs"></script>
<script type="text/javascript">
    ...
    var ChatProxy;
    function Connect() {
        ChatServerUrl = "http://localhost:49611/";
        var ChatUrl = ChatServerUrl + "signalr";
        //This will hold the connection to the signalr hub   
        SignalrConnection = $.hubConnection(ChatUrl, {
            useDefaultPath: false
        });
        ChatProxy = SignalrConnection.createHubProxy('ChatHub');
    }
    ...
</script>

下载存储库并尝试

这篇关于SignalR“访问XMLHttpRequest ..."ASP.NET MVC中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆