查看生产环境中的组件加载问题 ASP.NET 核心

View Component Loading Issue on Production ASP.NET Core

我正在开发一个要加载视图组件的应用程序。在本地机器上它没有任何问题或错误,但是当我进行部署时它无法正常工作并给我 500 错误。这是我的实现。

Jquery函数

function UPdateHistoryGrid() {
   
    $("#notification-history").empty();
    var _url = '@Url.Action("NotificationHistory", "Notification")';
    $.ajax({
        type: "GET",
        url: _url,           
        success: function (result) {
            $("#notification-history").html(result);
        },
        error(res) {
            console.log(res)
        }
    });
      
};

控制器动作方式

     public IActionResult NotificationHistory()
            {
                return ViewComponent("NotificationHistory");
            }

查看组件.cs

public class NotificationHistoryViewComponent : ViewComponent
    {
   
        protected readonly IHttpNetClientService _apiService;
        IUserProfileInfoProvider _userInfoProvider;

        public NotificationHistoryViewComponent(IHttpNetClientService HttpService,
            IUserProfileInfoProvider userInfo)
        {
            _apiService = HttpService;
            _userInfoProvider = userInfo;
        }
        public async Task<IViewComponentResult> InvokeAsync()
        {
            var model = new NotificationParentModel();           
            var NotificationApiJsonResult = await _apiService.GetAllAsync(Notification_API_URL.GetAllNotificationsHistory.GetEnumDescription(), _userInfoProvider.GetUserInfo().Id, _userInfoProvider.GetToken());
            var notificationData = JsonConvert.DeserializeObject<ResponseDTO<IEnumerable<GMDNotificationDTO>>>(NotificationApiJsonResult);
           
            model.NotificaitonList = notificationData.Data.ToList();           
            return await Task.FromResult((IViewComponentResult)View("NotificationHistory", model));
        }
    }

查看代码

@using GMDSuperAdmin.Helper
@model GMDSuperAdmin.Models.NotificationParentModel

<div class="notificationCard-Container text-container @(!Model.IsToday ? "mt-5" : "") bg-white px-0">
    <div class="position-relative">
        <h5 class="text-center py-3">Notification History</h5>
    </div>

    @{
        if (Model.NotificaitonList.Count() > 0)
        {
            foreach (var item in Model.NotificaitonList)
            {
                <div class="row message-row message-row-2 mx-0 py-1" id="clickable-row">
                   
                    <div class="row mx-0 main-notificationRow justify-content-between" id="translate-row" @*onclick="selectedNotification(this, @item.NOtificationId)"*@>
                        <div class="d-flex align-items-center py-2">
                            <div class="notification-list_img document-noti mx-2 mt-1 mx-lg-3">
                                <i class="fas fa-file-alt"></i>
                            </div>

                            <div class="notifierInfo">
                                <p class="message-paragraph mb-0">
                                    @item.NotificationDescription
                                </p>
                            </div>
                        </div>
                        
                        <div class="notification-time pt-1 pb-2 mx-2">
                            <p class="message-paragraph text-right mb-0">
                                @(DateTimeHelper.TimeAgo(item.CreatedDate))
                            </p>
                        </div>
                    </div>
                </div>
            }
        }
        else
        {
            <div class="row message-row py-1 mx-0" id="clickable-row">

                <div class="row mx-0 main-notificationRow" id="translate-row">
                    <div class="col-12 col-lg-12">
                        <p class="message-paragraph text-muted mb-0 text-center">
                            <b>No Notification Found!</b>
                        </p>
                    </div>
                </div>
            </div>
        }
    }

</div>

请将您的视图组件重命名为 defaultNotificatioHistory.cshtmlDefault.cshtml。有时它会在生产中出现自定义名称问题,因此推荐的方法是使用 Default.cshtml.

public class NotificationHistoryViewComponent : ViewComponent
{
    protected readonly IHttpNetClientService _apiService;
    IUserProfileInfoProvider _userInfoProvider;

    public NotificationHistoryViewComponent(IHttpNetClientService HttpService,
        IUserProfileInfoProvider userInfo)
    {
        _apiService = HttpService;
        _userInfoProvider = userInfo;
    }
    public async Task<IViewComponentResult> InvokeAsync()
    {
        var model = new NotificationParentModel();           
        var NotificationApiJsonResult = await _apiService.GetAllAsync(Notification_API_URL.GetAllNotificationsHistory.GetEnumDescription(),
            _userInfoProvider.GetUserInfo().Id, _userInfoProvider.GetToken());
        var notificationData = JsonConvert.DeserializeObject<ResponseDTO<IEnumerable<GMDNotificationDTO>>>(NotificationApiJsonResult);
       
        model.NotificaitonList = notificationData.Data.ToList();           
        return View(model); //// Change this ... 
    }
}