4

「HotTowel」シングルページアプリテンプレートを使用していますが、SignalRを機能させることができません。次のエラーが発生します。

Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8184/signalr/hubs

標準のMVC4シングルページアプリを使用して、非常にシンプルなハブを配置した一種の定型文を作成しました(ユーザーのオンラインカウンター)。すべてが正常に機能していました。

「HotTowel」に切り替えた後、動作を停止しました。John Papaに確認したところ、ルーティングを妨げるいくつかの問題について修正が行われることを知っていたため、Durandal側で確認するよう提案されました。

main.js:

require.config({
  paths: {
    "text": "durandal/amd/text",
    "signr": "../scripts/jquery.signalR-1.0.1.min"
  }
});
define(['durandal/app', 'durandal/viewLocator', 'durandal/system', 'durandal/plugins/router', 'services/logger', "signr"],
  function (app, viewLocator, system, router, logger, sigr) {

  // Enable debug message to show in the console
  system.debug(true);

  app.start().then(function () {
    toastr.options.positionClass = 'toast-bottom-right';
    toastr.options.backgroundpositionClass = 'toast-bottom-right';

    router.handleInvalidRoute = function (route, params) {
      logger.logError('No Route Found', route, 'main', true);
    };

    // When finding a viewmodel module, replace the viewmodel string 
    // with view to find it partner view.
    router.useConvention();
    viewLocator.useConvention();

    // Adapt to touch devices
    app.adaptToDevice();
    //Show the app by setting the root view model for our application.
    app.setRoot('viewmodels/shell', 'entrance');
  });
});

Global.asax.cs:

protected void Application_Start()
{
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    // Register the default hubs route: ~/signalr/hubs
    RouteTable.Routes.MapHubs();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

HotTowel \ index.cshtml:

<body>
  <div id="applicationHost">
    @Html.Partial("_splash")
  </div>

  @Scripts.Render("~/scripts/vendor")
    @if(HttpContext.Current.IsDebuggingEnabled) {
      <script type="text/javascript" src="~/App/durandal/amd/require.js" data-main="@Url.Content("~/App/main")"></script>
    } else {
      <!-- Remember to run the Durandal optimizer.exe to create the main-built.js  -->
      <script type="text/javascript" src="~/App/main-built.js"></script>
    }    
  <script type="text/javascript" src="~/signalr/hubs"></script>    
  @*<script src="~/App/services/hubs.js"></script>*@
</body>

(これはおそらくそれを置く場所ではないことを知っていますが、ここに置くまで、ソースに表示させることはできませんでした)-ちなみに、正しい方法を教えていただければ、感謝します

これをトラブルシューティングするのに役立つ他に何を教えてもらえますか?SignalRなどへのすべての参照があります...別のプロジェクトテンプレートを使用した実用的な例があります...

誰かが私を導くことができますか?

複製するのは簡単なはずです。

  1. 「HotTowel」テンプレートを使用するだけです
  2. すべてのSignalR参照を追加します
  3. 私のコードでテストする

DurandalかRequire.Jsのどちらかがこれを邪魔していると思います。誰かがその日を救うことができますか?:)

4

1 に答える 1

10

問題は、SignalRが最初にルートを登録する必要があることです。HotTowelテンプレートは、WebActivatorでPreApplicationStartMethodを使用して、「最初になる」ことを試みます。

これを修正するいくつかの方法..WebActivatorを利用できるので、次のようにApp_Startで新しいクラス(SignalRConfig.csなど)を作成できます。

[assembly: WebActivator.PreApplicationStartMethod(
typeof(SignalRConfig), "RegisterRoutes", Order = 1)]
public static class SignalRConfig
{
    public static void RegisterRoutes()
    {
        RouteTable.Routes.MapHubs();
    }
}

これにより、HotTowelの前にSignalRのルートが登録されます。これで処理されるので、global.asaxのMapHubsへの呼び出しを削除します。

または、HotTowelRouteConfig.csを開き、この属性を削除/コメントアウトします。

[assembly: WebActivator.PreApplicationStartMethod(
typeof(Empire.Web.App_Start.HotTowelRouteConfig), "RegisterHotTowelPreStart", Order = 2)]

次に、global.asaxに移動し、RouteTable.Routes.MapHubs()を呼び出した後、次を追加します。

HotTowelRouteConfig.RegisterHotTowelPreStart();

この後、彼らはうまくプレーするはずです。

于 2013-03-09T22:52:08.207 に答える