1

I have two separate project with NET CORE 6. One project contains all code - Controller, Models and so on. Other projects contains all View. Of course View has reference to code and Inject various data from Model to View. This is output of View (class library) project (with references to Code project).

class library project with View

And this is output of Code project (Console application) with EntryPoint, tuning services in DI container and so on.

output of Code project

Of course, if we try to start Code project without View we will receive, because this project know nothing about View

InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml

And, of course, if we try to start project with View we will receive

Unable to run your project. The "RunCommand" property is not defined.

Because this project know nothing about Entry Point, tuning services, DI container and so on.
Solution is something close, but I can not understand it, sorry. How is possible to start this web site?

  • 1
    If you want to separate your concerns in this way I believe you need a third project. Your main project will reference the View project _and_ the third project, and the View project will refrerence the third project as well. This way you can put code that is shared between your main project and View project in the third project to prevent circular dependencies. You should then be able to start your main project, which has a reference to your View project. In any case, you need to rethink your project structure. – TheHvidsten Jul 23 '22 at 17:50
  • 1
    You should use [IViewLocationExpander Interface](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razor.iviewlocationexpander?view=aspnetcore-6.0). For example see https://stackoverflow.com/q/39471627/5045688 – Alexander Petrov Jul 23 '22 at 18:12
  • thank for both advice. Will be try. –  Jul 23 '22 at 20:06
  • 1
    I'm with GT and that you need to review your structure. View and model separation is fine but there is a runtiminess wrapper that needs to tie them all together. This is issue is that you don't have a central binder of these two libraries to bring them together. Normally what's done is you have say a project that has rudimentary view structures and another has models. These are then included as dependencies in another project that implements the views (or extensions of them) along with the models. You need that. –  Jul 24 '22 at 07:40

1 Answers1