我的前端需要 Startup.cs 吗?

Do I need to have a Startup.cs on my Frontend?

这个问题与此相关:

我遇到的问题是,我正在尝试从一个名为 Frontend 的 ASP.NET (localhost:50000) 项目到另一个 ASP.NET MVC 项目(在同一个解决方案)(本地主机:60000)。

当我 运行 应用程序时,我在控制台中收到此错误消息:

The following errors occurred while attempting to load the app. - No assembly found containing an OwinStartupAttribute. - No assembly found containing a Startup or [AssemblyName].Startup class. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

当我添加 OWIN 启动时 class 我没有收到此错误消息:

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

我猜这是因为它试图在自己的项目中找到中心。

问题 1: 我的前端需要启动吗?

问题 2(如果问题 1 == 是): 我必须在 Startup 中将 SignalR 映射到我的后端吗?

编辑: index.html:

<!--Script references -->
    <link href="Content/bootstrap.min.css" rel="stylesheet">
    <script src="Scripts/jquery-1.9.1.min.js"></script>
    <script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
    <script src="http://localhost:50000/signalr/hubs" type="text/javascript"></script>

<!-- HEAD ^^^ -->

<!-- Deleted stuff -->

<!-- BODY  vvv -->
<script type="text/javascript">
        $(function () {
            var broadcaster = $.connection.loginHub;
            console.log("broadcaster: " + broadcaster)

            $.connection.hub.start().done(function () {

            });

        });
    </script>

重新创建我的前端项目并仅安装 Microsoft.AspNet.SignalR.JS 包后,我知道在控制台中收到此错误:

HTTP Error 404.0 - Not Found .... Requested URL
http://localhost:60000/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%5D&_=1443076975867

http://localhost:60000 是前端,所以我不确定为什么要求 URL。

检查 http://localhost:50000/signalr/hubs 时,我从 ASP.NET SignalR JavaScript Library v2.2.0 获得了一个网站,而且当我在控制台中打印出 broadcaster 时,它显示 broadcaster: [object Object] 所以某种连接似乎成立。

在您的后端项目中,您需要安装这些包:

PM> Install-Package Microsoft.AspNet.SignalR
PM> Install-Package Microsoft.Owin.Cors

它的 Startup class 应该是这样的:

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

在你的前端项目中你需要安装这个包:

PM> Install-Package Microsoft.AspNet.SignalR.JS

您也可以为 .NET 客户端安装 SignalR:

PM> Install-Package Microsoft.AspNet.SignalR.Client

您可能需要将客户端设置为指向您的实际中心位置,因为默认情况下它是当前服务器。

$.connection.hub.url = '<yourbackendurl>;

在调用 connection.hub.start 之前执行此操作,它应该适合您。