作为独立应用程序运行时找不到ASP.NET核心MVC视图



我有一个案例,ASP.NET Core 2.2应用程序的启动过程发生在库中。应用程序调用该库来构建和运行主机。

库中的安装代码将应用程序本身作为ApplicationPart添加到ASP.NET Core MVC中,以便应用程序也可以定义ASP.NET Core MVC控制器和视图。

我面临的问题是,当从visual studio运行或使用时,会发现应用程序中定义的视图

dotnet run application.csproj

但是在单独运行应用程序的CCD_ 1文件时找不到。

库和应用程序都有一个名为LibraryName.Views.dllApplicationName.Views.dll的附加视图程序集(自动生成),它们都包含内部的所有视图。

该应用程序的目标是.NET Framework 4.8

我在这里错过了什么?

过程的一些日志记录。控制器的名称是";CustomStatusDialogController";。

工作(dotnet运行):

Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[1]
Executing ViewResult, running view Index.
dbug: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[2]
The view path '/Views/CustomStatusDialog/Index.cshtml' was found in 8167.5568ms.
info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[4]
Executed ViewResult - view Index executed in 10570.57ms.

不工作(应用程序的.exe文件)

...
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[1]
Executing ViewResult, running view Index.
fail: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor[3]
The view 'Index' was not found. Searched locations: /Views/LogMessages/Index.cshtml, /Views/CustomStatusDialog/Index.cshtml, /Views/Shared/Index.cshtml, /Pages/Shared/Index.cshtml

谢谢。

如果您使用的是.NET Core 2.x,请尝试在项目文件中定义MvcRazorCompileOnPublish标志

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>

对于.NET Core 3.x,您可以添加CopyRazorGenerateFilesToPublishDirectory

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
</PropertyGroup>

让我知道这是否有助于

尝试在<PropertyGroup>内的库的.csproj文件中添加<AddRazorSupportForMvc>true</AddRazorSupportForMvc>

最新更新