我正在使用"HotTowel"单页应用程序模板,但我无法使SignalR正常工作。我得到以下错误:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8184/signalr/hubs
我有一个使用标准MVC 4单页应用程序的样板,有一个非常简单的集线器(用户在线计数器(。一切都很好。
当我切换到"HotTowel"后,它就停止工作了。我和John Papa核实了一下,他给了我一个检查杜兰达尔队的建议,因为他提到他知道一些干扰某些路线的问题即将得到解决。
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等的参考…我有一个使用另一个项目模板的工作示例。。。
有人能给我指路吗?
它应该足够容易复制:
- 只需使用"HotTowel"模板
- 添加所有SignalR参考
- 使用我的代码进行测试
我想要么是杜兰达尔,要么是要求。Js妨碍了这件事。有人能拯救这一天吗?:(
问题是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之后。路线。MapHubs((,添加:
HotTowelRouteConfig.RegisterHotTowelPreStart();
在这之后,他们应该打得很好。