JQuery 将 MVC 应用程序部署到服务器后出现文件 404(未找到)和 500(内部服务器错误)

JQuery File 404 (Not Found) and 500 (Internal Server Error) after deploying MVC app to Server

我看到很多人都在为同样的问题而苦苦挣扎,但是 none 似乎引导我朝着正确的方向前进 - 也许我理解不正确,或者我的情况有点不同 - 我创造了一个在本地完美运行的 MVC 应用程序,但一旦部署,我在控制台中收到以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found) : glyphicons-halflings-regular.woff2

Failed to load resource: the server responded with a status of 404 (Not Found) : /api/notifications

Failed to load resource: the server responded with a status of 500 (Internal Server Error) : BudgetingTool/Account/UserDetails?searchTerm=Peter+Pan&_=1506510511716

最后一个是最重要的,因为它用于 return 数据。其代码如下:

public ActionResult UserDetails(string searchTerm = null)
        {
            var data = UserManager.Users.FirstOrDefault(r => (r.FirstName + " " + r.LastName) == searchTerm);
            IEnumerable<SelectListItem> rolesList = null;
            IEnumerable<SelectListItem> organisationList = null;
            Organisation organisation = null;
            Department department = null;

            if (data != null)
            {
                var userOrganisations = _organisationRepo.GetUserAssignedOrganisation(data.Id);
                organisation = _repository.FindOrganisationById(data.OrganisationId);
                department = _departmentRepo.FindDepartmentById(organisation.DepartmentId);
                rolesList = ShowAllowedRoles(data.Id);
                organisationList =
                    _repository.GetOrganisationsInHierarchyOrder(0,0,0).Select(r => new SelectListItem
                    {
                        Selected = userOrganisations.Any(x => x.OrganisationId == r.OrganisationId) ? true : false,
                        Text = r.Name,
                        Value = r.OrganisationId.ToString()
                    });
                //SelectedOrganisations = OrganisationList.Where(x => x.Selected == true).ToList().Select(r=> new List<string>{ r.Value});
            }
            else
            {
                organisationList = _repository.GetOrganisationsInHierarchyOrder(0, 0, 0).GroupBy(m => m.Name).Select(y => y.First()).Select(r => new SelectListItem
                {
                    Selected = false,
                    Text = r.Name,
                    Value = r.OrganisationId.ToString()
                });
                rolesList = GetRolesList();
            }

            var model = new UpdateUserInfoVM
            {
                Id = data == null ? string.Empty : data.Id,
                FirstName = data == null ? string.Empty : data.FirstName,
                LastName = data == null ? string.Empty : data.LastName,
                Email = data == null ? string.Empty : data.Email,
                Allowbudgetdeletion = data?.Allowbudgetdeletion ?? false,
                LockoutEnabled = data?.LockoutEnabled ?? false,
                //OrganisationId = data == null ? string.Empty : data.OrganisationId,
                OrganisationId = data?.OrganisationId ?? 0,
                Organisations = organisationList,
                Roles = rolesList,
                AccessFailedCount = data?.AccessFailedCount ?? 0,
                LockoutEndDateUtc = data?.LockoutEndDateUtc,
                AllowEditing = data.AllowEditing,
                DistrictOrUrban = data?.DistrictOrUrban,
                departmentId = organisation.DepartmentId,
                DepartmentType = department.Name
                //SelectedOrganisations = SelectedOrganisations
            };

            return PartialView("_ShowUserInfo", model);
            //return Request.IsAjaxRequest() ? PartialView("_ShowUserInfo", model) : PartialView("_ShowUserInfo", model);
        }

当我在控制台中点击 link 错误时,显示的是这个(好像没有传递任何值???

Server Error in '/BudgetingTool' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

我只是不明白它如何在我的本地运行得如此完美,但在发布然后部署到远程服务器时却不行。

该文件夹是一个虚拟目录,包含项目文件的文件夹与 "Everyone" 和 "IIS_IUSR" 共享 - 两者都具有 read/write 权限。

人们可以寻找的最一般的东西是什么?我确定它很简单,但就是找不到它是什么:(

在正确方向上的任何帮助都会有很大帮助。

谢谢!

DevOps 中有一句谚语:"It doesn't work, unless it works somewhere else than your development machine"。在这里,您的代码未通过该测试。一个开发箱是一个原始环境,你把它设置成它应该的样子,并确保你的本地数据库有绝对正确的数据,等等。现实世界是不那么宽容的,当你从数据库中查询东西时,如果您没有正确编码,您的生产环境会让您感觉良好。

也就是说,无论何时您从数据库中查询某些内容,您都必须考虑到您正在查询的内容未被找到。即使它 应该 存在,墨菲定律也表明它在某个时候不会存在。当您的查询没有返回任何内容时,该变量将被设置为 null,如果您没有在代码中进行正确的 null 检查,您将得到这样的 NullReferenceExceptions。

简单地说,NullReferenceException 意味着您试图访问类型实例的有效成员,但该实例实际计算为 null,而 null 没有该特定成员。例如:

organisation = _repository.FindOrganisationById(data.OrganisationId);
department = _departmentRepo.FindDepartmentById(organisation.DepartmentId);

在这里,您假设 Organization 实例存在,但它很可能不存在。如果你有一个 Organization 的实例,那么尝试检索 organization.DepartmentId 的值是完全有效的,但是如果你的 organization 变量实际上是 null,那么,null 没有 [=17] =] 成员和热潮:NullReferenceException.

任何时候你有任何可能为空的变量,假设它会是并相应地设计你的代码。