【问题标题】:Error: Failed to invoke 'addComment' due to an error on the server错误:由于服务器上的错误,无法调用“addComment”
【发布时间】:2020-12-07 21:17:31
【问题描述】:

我有一个常规应用程序。他的选择之一是发表评论。当您单击“发布”按钮时,将弹出一个错误。错误:由于服务器上的错误,无法调用“addComment”。 与集线器的连接已建立。 同样在配置设置中,添加了 SingalR 并安装了集线器的端点。 如何解决?

索引.cshtml

@model Blog111.Models.BlogViewModels.PostViewModel
@{
   Layout = "_HomeLayout";
   ViewData["HeaderImage"] = $"../../UserFiles/Blogs/{Model.Post.ID}/HeaderImage.jpg";
   ViewData["HeaderTitle"] = Model.Post.Title;
   ViewData["AuthorName"] = $"{Model.Post.Creator.FirstName} {Model.Post.Creator.LastName}";
   ViewData["PostDate"] = Model.Post.Update.ToString("MMMM d, yyyy");
   ViewData["AuthorId"] = Model.Post.Creator.Id;
}

<article>
<div class="container">
    <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
            @Html.Raw(Model.Post.Content)
        </div>
    </div>
    <div class="row mt-5">
        <div class="col-lg-8 col-md-10 mx-auto">
            <h3 class="mb-5">@Model.Post.Comments.Count() Comments</h3>
            <ul class="comment-list comment-top" list="@Model.Post.Comments" count="0" id="comlist">
                @foreach (var comment in Model.Post.Comments.Where(comment => comment.Parent is null))
                {
                    <li class="comment" style="margin-top: 30px;">
                        <div class="comment-body">
                            <h4 class="commenter-name">@comment.Poster.FirstName @comment.Poster.LastName</h4>
                            <div class="comment-date">@comment.Create.ToString("MMMM d, yyyy hh:mm:tt")</div>
                            <p class="comment-message">@comment.Content</p>

                        </div>


                    </li>
                    <hr />
                }
            </ul>
            @if (User.Identity.IsAuthenticated)
            {
                <div>
                    <form>
                        <input asp-for="Post.ID" readonly hidden id="postId" />
                        <input asp-for="Comment.Poster.FirstName" readonly hidden id="uF" />
                        <input asp-for="Comment.Poster.LastName" readonly hidden id="uL" />
                        <div class="form-group">
                            <label for="comment">Comment</label>
                            <textarea asp-for="Comment.Content" class="form-control" rows="3" required id="content"></textarea>
                            <button type="submit" class="btn btn-outline-primary post-btn" id="send">Post</button>
                        </div>
                    </form>
                </div>
            }
            else
            {
                <a class="reply-btn btn" asp-area="Identity" asp-page="/Account/Login" asp-route-ReturnUrl="~/Post/@Model.Post.ID">Login to comment this Post</a>
            }
        </div>
    </div>
</div>
</article>
<script src="~/js/signalr/dist/browser/signalr.min.js"></script>
<script src="~/js/sendComments.js"></script>

sendComments.js

"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/comments").build();
document.getElementById("send").disabled = true;

connection.on("addComment", function (post, user, message) {
console.log("asdasdasd");
var item = "<div class=\"comment-body\"><h4 class=\"commenter-name\">".concat(user).
    concat("</h4>").concat("<div class=\"comment-date\">").concat(date).concat("</div>").concat("<p class=\"comment-message\">")
    .concat(message).concat("</p>").concat("</div>");
var li = document.createElement("li");
li.innerHTML = item;
document.getElementById("comlist").appendChild(li);
});
connection.start().then(function () {
document.getElementById("send").disabled = false;
}).catch(function (err) {
return console.error(err.toString());
});

document.getElementById("send").addEventListener("click", function (event) {
var uF = document.getElementById("uF").value;
var uL = document.getElementById("uL").value;
var post = document.getElementById("postId").value;
var user = uF.concat(uL);
var message = document.getElementById("uF").value;

connection.invoke("addComment", post, user, message).catch(function (err) {
    return console.error(err.toString());
});
event.preventDefault();
});

CommentsHub.Cs

using Blog111.Data;
using Blog111.Entities;
using Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.SignalR;
using Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog111.Hubs
{
public class CommentsHub : Hub
{
    private readonly IPostService postService;
    private readonly UserManager<User> userManager;
    ApplicationDbContext db;

    public CommentsHub(IPostService postService, UserManager<User> userManager, ApplicationDbContext <User> db)
    {
        this.postService = postService;
        this.userManager = userManager;
    }
   public async Task addComment(int postId, string user, string message)
    {
        string first = user.Split(' ')[0];
        string second = user.Split(' ')[1];
        Comment comment = new Comment
        {
            Post = db.Posts.FirstOrDefault(post => post.ID == postId),
            Poster = db.Users.FirstOrDefault(user => user.FirstName == first && user.LastName == second),
            Create = DateTime.Now,
            Content = message
        };
        await Clients.All.SendAsync("addComment", postId, user, message);
        db.Comments.Add(comment);
        await db.SaveChangesAsync();
    }
}

}

【问题讨论】:

    标签: asp.net asp.net-core signalr


    【解决方案1】:

    更改调用函数的名称。您的 Hub 方法和调用的同名函数可能就是您出现此错误的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2014-06-25
      相关资源
      最近更新 更多