top of page
  • Mark Milbourne

Asp.net ViewComponent can’t find Default.cshtml

Recently while I was working on a project I hit this error;

An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Components/ExpirationNotification/Default' was not found. The following locations were searched:
/Views/Home/Components/ExpirationNotification/Default.cshtml
/Views/Shared/Components/ExpirationNotification/Default.cshtml
/Pages/Shared/Components/ExpirationNotification/Default.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)

Stack Query Cookies Headers
InvalidOperationException: The view 'Components/ExpirationNotification/Default' was not found. The following locations were searched: /Views/Home/Components/ExpirationNotification/Default.cshtml /Views/Shared/Components/ExpirationNotification/Default.cshtml /Pages/Shared/Components/ExpirationNotification/Default.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)
Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult.ExecuteAsync(ViewComponentContext context)
Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentInvoker.InvokeAsync(ViewComponentContext context)
Microsoft.AspNetCore.Mvc.ViewComponents.DefaultViewComponentHelper.InvokeCoreAsync(ViewComponentDescriptor descriptor, object arguments)
AspNetCore.Views_Shared__Layout+<>c__DisplayClass28_0+<<ExecuteAsync>b__1>d.MoveNext() in _Layout.cshtml 

For my own documentation; The answer to this problem was how Asp.net uses naming conventions to find various things in the project.

I had names the folder with the word Area on the end. Yet the class didn’t have that. So Asp.net was looking for the view ExpirationNotification/Default.cshtml. Which doesn’t exist.

Solution: Make the class and the folder the same name prefixes. Like this {Foldername}ViewComponent

I found a post on the forums.asp.net about this, but the solution wasn’t obvious and the thread is closed. So I posted it here for someone else to benefit from.

25 views0 comments
bottom of page