OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.23k stars 2.34k forks source link

"Blank Recipe" theme and error at first running: "InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'OrchardCore.Diagnostics.ViewModels.HttpErrorShapeViewModel', but this ViewDataDictionary instance requires a model item of type 'WebApp.Models.ErrorViewModel'" #15267

Open MarGraz opened 5 months ago

MarGraz commented 5 months ago

Describe the bug

I followed the official documentation to create a new Orchard Core Web App and installed the OrchardCore.Application.Cms.Targets and OrchardCore.Application.Targets NuGet packages as suggested in the second GIF image. After configuring the Web App to use a SQL Server on localhost and starting the app, I encountered an error on the front end:

An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'OrchardCore.Diagnostics.ViewModels.HttpErrorShapeViewModel', but this ViewDataDictionary instance requires a model item of type 'WebApp.Models.ErrorViewModel'. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)

Despite using a blank recipe, which I thought might naturally lead to front-end errors, I followed the official guide to create my own theme (using the Code Generation Templates) and added a Layout.cshtml with only an "Hello world" text. I also updated the .csproj file to use the Microsoft.NET.Sdk.Razor SDK, but the error persisted.

To Reproduce

Steps to reproduce the behavior:

  1. Create a new blank ASP.NET Core Web Application (.NET 8) in VS 2022 v. 17.8.6;
  2. Install OrchardCore.Application.Cms.Targets v. 1.8.2 and OrchardCore.Application.Targets v. 1.8.2 from the stable NuGet feed, as shown in the second GIF image;
  3. Run the Web App with CTRL + F5 and configure it to use a localhost SQL Server;
  4. Attempt to access the frontend to encounter the described error.

Expected behavior

Using a "Blank Recipe" I expected not to see a template, but also not to encounter an exception. Ideally, a simple text page should appear, guiding the user to consult the documentation for creating a custom theme and locating the "Admin" panel.

Endpoint

Name Value
Display Name OrchardCore.Diagnostics.Controllers.DiagnosticsController.Error (OrchardCore.Diagnostics)
Route Pattern Error/{status?}
Route Order 71

Route Values

Variable Value
action Error
area OrchardCore.Diagnostics
controller Diagnostics
status 404

Exception details

System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'OrchardCore.Diagnostics.ViewModels.HttpErrorShapeViewModel', but this ViewDataDictionary instance requires a model item of type 'WebApp.Models.ErrorViewModel'.
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(Object value)
   at lambda_method93(Closure, ViewDataDictionary)
   at Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.Activate(Object page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultFilters>g__Awaited|28_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddleware.Invoke(HttpContext httpContext, Boolean retry)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at OrchardCore.Liquid.ScriptsMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatch|10_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
   at OrchardCore.Diagnostics.DiagnosticsStartupFilter.<>c__DisplayClass3_0.<<Configure>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.<>c__DisplayClass7_0.<<CreateHandler>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at OrchardCore.ContentPreview.PreviewStartupFilter.<>c.<<Configure>b__0_1>d.MoveNext()
--- End of stack trace from previous location ---
   at OrchardCore.Modules.ModularTenantRouterMiddleware.Invoke(HttpContext httpContext)
   at OrchardCore.Modules.ModularTenantContainerMiddleware.<>c__DisplayClass4_0.<<Invoke>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
   at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
   at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
   at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
   at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
   at OrchardCore.Modules.ModularTenantContainerMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Screenshots

image

MarGraz commented 5 months ago

I noticed that there is a discrepancy in the official documentation. The second GIF image recommends using the NuGet packages OrchardCore.Application.Cms.Targets and OrchardCore.Application.Targets. However, another section of the documentation here advises using OrchardCore.Application.Cms.Targets and OrchardCore.Application.Cms.Core.Targets.

After updating my WebApp to reference OrchardCore.Application.Cms.Targets and OrchardCore.Application.Cms.Core.Targets, both version 1.8.2, I still encounter the same error, with or without my theme project referenced or not.

MarGraz commented 5 months ago

The issue starts with creating a new blank "ASP.NET Core Web Application" in VS 2022 using the manual method. This process, unlike the automated template, results in Visual Studio automatically adding Controllers and Views folders with files, that subsequently cause errors in Orchard Core, specifically because of the Error.cshtml view model. Excluding the Controllers and Views folders from the project fix this issue.

I noticed that the official documentation does not mention to remove Controllers and Views folders when starting a project with the manual method, right? 🤔

  1. Could you specify where in the documentation the "manual method" is mentioned? I would like to add a note about removing the Controllers, Views and Models folders from the Web App, assuming this is the correct approach 😊
  2. Regarding my previous comment, can you confirm the discrepancy between the GIF image and the documentation? If confirmed, I think it should be fixed
MikeAlhayek commented 5 months ago

@MarGraz is there a controller in your web project that handle errors? You may have Error.cshtml in the shared folder which should be removed. Also, if you have a controller in your webapp that handles errors, delete it

MarGraz commented 5 months ago

@MikeAlhayek yes, thank you. I solved as I explained here. What do you think about the two questions I asked?

MikeAlhayek commented 5 months ago

@MarGraz sorry I did not see you follow up comments. Glad you fixed the issues.

Yes feel free to submit a PR to modify the documentation to mention removing the views and controller if existing.

MarGraz commented 5 months ago

@MikeAlhayek ok, I have just two questions:

Thank you

MikeAlhayek commented 5 months ago

Regarding https://github.com/OrchardCMS/OrchardCore/issues/15267#issuecomment-1932152014 I posted, which library is the correct one to be used? OrchardCore.Application.Cms.Core.Targets or OrchardCore.Application.Cms.Targets can be used. It depends on the scenario you want to achieve. I would suggest to use OrchardCore.Application.Cms.Targets and change it if needed

Where in the documentation is the "manual method" mentioned? Is it only on this page?

In this section https://docs.orchardcore.net/en/main/docs/getting-started/templates/#from-visual-studio-manual-way we should add a section to mention removing the views and controllers.