如何让asp.net核心应用在当前项目中查找视图文件?

How to make asp.net core application to look for view files in the current project?

我的解决方案中有两个项目。 其中之一 (main) 是应用程序入口点。另一个 (api) 处理其中一个业务流程,在 99% 的情况下需要 JSON 通信。 但是,对于 1% 的情况,我需要完整的 HTML 响应,这就是问题所在。我创建了一个控制器和一个视图,但应用程序似乎在 main 项目中寻找视图。

我希望它在解决方案中使用以下文件结构:

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

但我似乎需要将视图从 /API/Views/Test/Action.cshtml 复制到 /Main/Views/Test/Action.cshtml/Main/Views/Shared/Action.cshtml。这拆分了我的应用程序的内部逻辑:(

您不能(目前,至少)在 ASP.NET 核心项目之间共享视图。在 ASP.NET Core 2.1(预览版)中,您可以通过将视图添加到两个项目都可以引用的 class 库来共享视图。

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.

目前,您只需复制每个项目中的视图并将其放在您的路线图上,以便在 2.1 发布后将它们分解出来,然后您就可以升级了。

您可以使用您的 DLL 保留您的视图。 只需确保 build/deployment 在根位置复制 'views' 文件夹。