[英]ASP.NET Core MVC View not found when running as a standalone application
我有一個案例,當 ASP.NET Core 2.2 應用程序的啟動過程發生在庫中時。 應用程序調用該庫來構建和運行主機。
庫中的設置代碼將應用程序本身作為 ApplicationPart 添加到 ASP.NET Core MVC,以便應用程序也可以定義 ASP.NET Core MVC 控制器和視圖。
我面臨的問題是從 Visual Studio 運行或使用時找到了應用程序中定義的視圖
dotnet run application.csproj
但單獨運行應用程序的.exe
文件時找不到。
庫和應用程序都有一個額外的視圖程序集(自動生成),名稱分別為LibraryName.Views.dll
和ApplicationName.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>
>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.