0

I have two projects in my solution. One (main) of them is the application entry point. The other one (api) handles one of the business processes, which require JSON communication in 99% cases. However, for 1% of cases I need full HTML response, and here is the problem. I created a controller and a view, but it seems that application looks for the view in the main project.

I want it to work with the following file structure within the solution:

/Main/ (and the content of main application) /API/Controllers/TestController.cs (contains definition of Action()) /API/Views/Test/Action.cshtml

But it seems I need to copy the view from /API/Views/Test/Action.cshtml to /Main/Views/Test/Action.cshtml or /Main/Views/Shared/Action.cshtml. That splits internal logic of my application :(

  • Have you checked your route configurations? It's probably that the route configs are causing the application look in the main project for the views. – Clint Feb 19 '18 at 14:54
  • Possible duplicate of [Is it possible to access MVC Views located in another project?](https://stackoverflow.com/questions/24341336/is-it-possible-to-access-mvc-views-located-in-another-project) – Clint Feb 19 '18 at 14:55
  • Not perhaps 100% a duplicate, but it does break it down into enough detail to be helpful I think. – Clint Feb 19 '18 at 14:55

2 Answers2

1

You can keep your views with your DLL. Just ensure on build/deployment copy 'views' folder at root location.

akhileshcoer
  • 162
  • 10
0

You cannot (currently, at least) share views between ASP.NET Core projects. In ASP.NET Core 2.1 (in preview), you will be able to share views by adding them to a class library that both projects can reference.

Razor UI in a class library

ASP.NET Core 2.1 will make it easier to build and include Razor based UI in a library and share it across multiple projects. A new Razor SDK will enable building Razor files into a class library project that can then be packaged into a NuGet package. Views and pages in libraries will automatically be discovered and can be overridden by the application. By integrating Razor compilation into the build, the app startup time is also significantly faster, while still allowing for fast updates to your Razor views and pages at runtime as part of an iterative development workflow.

For the time being, you'll just have to duplicate the views in each project and put it on your road map to factor them out once 2.1 is released and you can upgrade.

Community
  • 1
  • 1
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444