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

p587 - Exception because <partial name="_Employee" ... not found #41

Closed johntempest closed 1 year ago

johntempest commented 1 year ago

"An unhandled exception occurred while processing the request." Displayed on page localhost:5001/packtfeatures/employees after pressing the button "Contact our employees" on the main page.

Chapter: 13 Page Number: 587 Section Title: Creating a Razor class library Step Number: 9 Problem to fix: An Exception occurs due to <partial name="_Employee" not found when Northwind.Web is started as per step 3 on page 588 Suggested solution: Change "_Employee" to "Employee" [i.e. remove the underscore character at the start of the string.] Other useful information e.g. OS, coding tools, and so on: Windows 11, Microsoft Visual Studio 1922 Submitted by John Tempest (john_tempest@blueyonder,co,uk)

Full text of exception as appears on Chrome main window: An unhandled exception occurred while processing the request. InvalidOperationException: The partial view '_Employee' was not found. The following locations were searched: /Areas/PacktFeatures/Pages/_Employee.cshtml /Areas/PacktFeatures/Pages/Shared/_Employee.cshtml /Areas/PacktFeatures/Views/Shared/_Employee.cshtml /Pages/Shared/_Employee.cshtml /Views/Shared/_Employee.cshtml Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)

Stack Query Cookies Headers Routing InvalidOperationException: The partial view '_Employee' was not found. The following locations were searched: /Areas/PacktFeatures/Pages/_Employee.cshtml /Areas/PacktFeatures/Pages/Shared/_Employee.cshtml /Areas/PacktFeatures/Views/Shared/_Employee.cshtml /Pages/Shared/_Employee.cshtml /Views/Shared/_Employee.cshtml Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output) Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count) AspNetCoreGeneratedDocument.Areas_PacktFeatures_Pages_Employees.ExecuteAsync() in Employees.cshtml +

Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|30_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

markjprice commented 1 year ago

The naming convention for shared files like layouts and partial views is to prefix with underscore. Your problem was caused by you naming your file Employee.cshtml instead of _Employee.cshtml. Instead of removing the _ from the <partial> element name, you should add it to the name of the file. The exception you got tells you one of the paths that you should use to find it: /Pages/Shared/_Employee.cshtml

markjprice commented 1 year ago

image

markjprice commented 1 year ago

I have added some improvements for the next edition that I hope will help to avoid your issue in future:

https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/improvements.md#page-533---building-websites-using-aspnet-core

https://github.com/markjprice/cs11dotnet7/blob/main/docs/errata/improvements.md#page-586---creating-a-razor-class-library-page-587---implementing-a-partial-view-to-show-a-single-employee

johntempest commented 1 year ago

Mark Thank you for responding to my issue so quickly. As you point out, I failed to notice the initial underscore character in the naming of the file created in the shared folder of the Northwind.Razor.Employees project. I’ve followed your advice and restored the code and file names to how they appeared in the 7th edition, and starting the Northwind.Web up does indeed display the employees page as shown in the book without causing the runtime exception I initially experienced.

I have also read your comments and clarifications proposed for the next edition and find them both enlightening and useful. Thank you. For me, the issue I raised can acceptably closed now. Regards John Tempest

Sent from Mail for Windows

From: Mark J. Price Sent: 14 March 2023 12:04 To: markjprice/cs11dotnet7 Cc: johntempest; Author Subject: Re: [markjprice/cs11dotnet7] p587 - Exception because <partialname="_Employee" ... not found (Issue #41)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>