markjprice / cs11dotnet7

Repository for the Packt Publishing book titled "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" by Mark J. Price
566 stars 206 forks source link

InvalidOperationException: The view 'Customers' was not found. #47

Open johntempest opened 1 year ago

johntempest commented 1 year ago

Chapter: 15 Page Number: 694 Section Title: Getting customers as JSON in the controller/Starting the web service and MVC client projects Step Number: 4 Problem to fix: After entering UK in the Select Country box on the Home Web Page and pressing the associated button to select the appropriate customers. Chrome displays "InvalidOperationException: The view 'Customers' was not found. The following locations were searched: /Views/Home/Customers.cshtml /Views/Shared/Customers.cshtml /Pages/Shared/Customers.cshtml Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations)

Suggested solution: Other useful information e.g. OS, coding tools, and so on: The following locations were searched: /Views/Home/Customers.cshtml /Views/Shared/Customers.cshtml /Pages/Shared/Customers.cshtml Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations) Chrome and Visual Studio 2022

All code up to page 694 had been entered as in the book, and runs up to that point had been successful after clearing typos. I've rechecked the code I've entered as against the book. I noted that what appears to be the "missing" view in Northwind.Mvc.Views/Home/Customers.cshtml has at the head of the code: @model IEnumerable which may be why the Customers view is not found. However changing "Customer" to "Customers" produces a red squiggle underlining in the code editor suggesting "Customers" is not found in PACKT.SHARED I have a vague memory that a similar error was encountered earlier and had to be rectified by creating a set from the single-version named table in the database to create an appropriate plural-named collection -- but I can't recall the exact details. I couldn't find anything in the errata file. I've run the code from the GitHub repository which does work properly, but I've yet to find what I've done wrong in my code and could use some suggestions as to where to find the error.

markjprice commented 1 year ago

If you are getting that error saying that it cannot find /Views/Home/Customers.cshtml then you either have not completed Step 4, as shown in the screenshot below, or you have created a Razor Page instead of a Razor View. image Did you put a @page at the top of the file contents? If so, remove it.

markjprice commented 1 year ago

You might find the following useful: https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/improvements.md#page-533---building-websites-using-aspnet-core

johntempest commented 1 year ago

I did manage to get Northwind.Mvc to display the customers for the selected country as indicated in the 7e book. But then I did the tasks in the optional part of the chapter detailed in the GitHub pages. Then after trying out displaying the customers again, I encountered the same unhandled exception of not finding the Customers view in any of the three places indicated. I put a breakpoint in the HomeController.cs code where the HTTpClient is created for Northwind.WebApi. The exception occurred after the response was received from the Northwind.WebApi server (all the customer details were shown on the terminal window for that server). I could only assume that the "model" after being processed by the ".ReadFromJsonAsync<IEnumerable>()" did not somehow fit with the structure of the Customers.cshtml file. I then checked the Customers.cshtml file character by character with the code in the repository compared to my code - to no avail. Eventually I decided to replace the code in my file with the repository version of the Customers.cshtml and Northwind.Mvc.csproj files using cut and paste of the complete files. This seems to have cleared the problem. It may be possible that my Customers.cshtml file may have had some hidden character in it which effectively corrupted the file. This seems to be the only explanation for the exception occurring even though I spent many hours comparing my code with that in the repository. I have now completed Chapter 15 of the 7th edition and am proceeding to Chapter 16.