我有一个使用 SignalR 的 ASP.NET MVC 应用程序。当处理 SignalR 的客户端脚本是内联的时,它工作正常,但如果我将脚本提取到 .js 文件,它不会接收来自服务器的调用。
@model LiveContentSample.Models.Entry
@{
ViewBag.Title = "Details SignalR";
ViewBag.EntryId = Model.Id;
}
<h2>Details with PartialView SignalR Comments</h2>
<fieldset>
<legend>Entry</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.Title)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Title)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Content)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Content)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>
@Html.Action("_GetForEntryWithSignalR", "Comment", new { entryId = @Model.Id })
@section scripts {
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.js"></script>
<script src="~/Scripts/jquery.signalR-1.1.3.min.js"></script>
<script src="~/signalr/hubs"></script>
@* <script type="text/javascript" src="~/Scripts/CommentsSignalR.js"></script>*@
<script>
$(function () {
var entryId = '@ViewBag.EntryId';
var commentHub = $.connection.commentHub;
commentHub.client.send = function (content) {
$("#comments-list").append("<li>" + content + "</li>");
};
$.connection.hub.start().done(function () {
commentHub.server.register(entryId);
$("#add-comment").click(function () {
commentHub.server.addComment(entryId, $("#content").val());
});
});
});
</script>
}
但是,如果我将最终脚本移到 .js 文件中,它将不起作用。有任何想法吗?