【问题标题】:How to avoid getting 500 error when using Component.InvokeAsync method?使用 Component.InvokeAsync 方法时如何避免出现 500 错误?
【发布时间】:2020-06-22 01:50:16
【问题描述】:

我想在布局中使用视图组件,但是当我在布局中放入以下代码时

 @await Component.InvokeAsync("LastNotifications");

我收到此错误:

LastNotifications 是我传递给 Invoke 方法的正确值,编译器在检测问题时不会抛出任何错误。

这是我拥有的最后一个通知视图组件类:

 public class LastNotificationsViewComponent : ViewComponent
    {

        private readonly INotificationQueries _notificationQueries;
        private readonly IUserRepository _userRepository;
        public LastNotificationsViewComponent(INotificationQueries notificationQueries, IUserRepository userRepository)
        {
            _notificationQueries = notificationQueries;
            _userRepository = userRepository;
        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var currentUser = await _userRepository.GetAsync(HttpContext.User.Identity.Name);
            var model = await _notificationQueries.GetMyLastNotification(currentUser.Id);
            return View(model);
        }

    }

【问题讨论】:

  • 我们如何知道您是否未在问题中发布异常? 500 = 内部服务器错误,总是在未捕获到异常时发生

标签: c# asp.net asp.net-core .net-core asp.net-core-mvc


【解决方案1】:

此错误可能是由于查看组件页面的文件夹位置错误。

请检查您的view Components path 是否正确。

查看搜索路径有以下三种方式:

  • /Views/{控制器名称}/Components/{视图组件名称}/{视图 姓名}

  • /Views/Shared/Components/{视图组件名称}/{视图名称}

  • /Pages/Shared/Components/{查看组件名称}/{查看名称}

您可以参考this

【讨论】:

    猜你喜欢
    • 2016-11-14
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多