0

I see there are many people struggling with this same issue, but none seems to lead me in the right direction - Perhaps I dont understand correctly, or my situation is just a little different - I have created an MVC app that runs perfectly on my local, but once deployed, I get the following errors in the Console :

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

The last one is the most important as this is used to return data. Its code is as follows:

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);
        }

When I click on the link for the error in the console, this is what displays(as if there is no value passed through ???

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.

I just dont understand how it can work so perfectly on my local, but not when published and then deployed to a remote server.

The folder is a virtual directory and the folder that contains the projects files is shared with "Everyone" and "IIS_IUSR" - Both have read/write permission.

What are the most general things that one can look for ? Im sure its something so simple, but just can not find out what it is :(

Any help in the right direction would be of great help.

Thanks!

AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • At least for the "woff2" file, it's surely a problem with a missing/wrong mime type within the IIS configuration: https://stackoverflow.com/a/28955302/265165 – thmshd Sep 27 '17 at 11:19
  • Your _NullReferenceException_ is the source of the other issue. Probably some Query doesn't return the expected data, this might be a connection error to the Database, missing Permissions, Failed Migration etc. we cannot guess what's wrong. Enable Exception logging to find the Error in the Logs – thmshd Sep 27 '17 at 11:25
  • Thanks for the reply - I tried adding that to my web.config, but doesnt make a difference :( Is teh exception logging on the browser ? How do I enable this and where do I find the logs ? – AxleWack Sep 27 '17 at 11:29
  • The Exception is on the Server. Logging in ASP.NET is a separate topic, I can't give any details here (Here and there, the usage of a tool called ELMAH is suggested to get more details easily, but yeah, just google it). – thmshd Sep 27 '17 at 11:41
  • Thanks! So I feel a bit stupid.... The issue with point 3 was actually because of a data issue. I pulled the DB off the server and used it with my local development and picked up that incorrect I'd were used(this is because of previous existing data that should have been removed). As for the 2 other points, im still a little stumped. But thanks for trying to help nevertheless. (Its no wonder none of the other links to found didnt work :-/) – AxleWack Sep 27 '17 at 13:54
  • PS: How I picked this up, was I deployed my App and DB the Azure and it all worked! The moment I used the DB that was on the Server, it gave the issue in point 3 as well. This is how I realized it MUST be Data related. – AxleWack Sep 27 '17 at 13:55

1 Answers1

0

There's a saying in DevOps that goes: "It doesn't work, unless it works somewhere else than your development machine". Here, your code fails that test. A development box is a pristine environment, you set it up exactly how it should be, and ensure that your local database has absolutely correct data, etc. The real world is much less forgiving, and when you do stuff like query things from a database, your production environment will smack you good if you haven't coded that properly.

Namely, any time you query something from a database, you must account for what you're querying for not being found. Even if it should be there, Murphy's Law says it won't be at some point. When nothing is returned from your query, the variable will be set to null, and if you're not doing proper null-checks in your code, you'll get NullReferenceExceptions like this.

Simply, a NullReferenceException means that you attempted to access a valid member of a type instance, but that instance actually evaluated to null, and null doesn't have that particular member. For example:

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

Here, you're assuming that the Organization instance exists, but it may very well not. Attempting to retrieve the value of organization.DepartmentId is perfectly valid if you have an instance of Organization, but if your organization variable is actually null, well, null doesn't have a DepartmentId member and boom: NullReferenceException.

Any time you have any variable that could potentially be null, assume it will be and design your code accordingly.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I am actually going to mark yours as the answer - Because as I mentioned now in a comment on my post, it was data related - and co-incidentally, the error occurred exactly on the _departmentRepo.FindDepartmentById' line of code. As for the other 2 items, I will just need to check what URLS are being returned and Im sure that will be why they are not found.... I hope. Thanks @Chris! – AxleWack Sep 27 '17 at 14:00