简体   繁体   English

ASP.NET Core找不到视图

[英]ASP.NET Core could not find views

I have an ASP.NET Core application. 我有一个ASP.NET核心应用程序。 The application needs to be started by windows service. 该应用程序需要由Windows服务启动。 When the service runs the application, I'm having the following error: 当服务运行应用程序时,我遇到以下错误:

InvalidOperationException: The view 'Index' was not found. The following locations were searched:
 /Views/Home/Index.cshtml
 /Views/Shared/Index.cshtml
EnsureSuccessful
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext

However, if I run the application by clicking on the exe-file, everything seems to be normal. 但是,如果我通过单击exe文件来运行应用程序,一切似乎都是正常的。 I double checked, the service had enough permissions, and the views are in the right place. 我仔细检查过,服务有足够的权限,视图在正确的位置。

BUT! 但! I had a situation when the service was looking for another file somewhere in win32 folder, because I had made a mistake and used Directory.GetCurrentDirectory() instead of Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) to find the current folder. 我有一种情况,当服务在win32文件夹中的某个地方寻找另一个文件,因为我犯了一个错误并使用Directory.GetCurrentDirectory()而不是Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)来查找当前文件夹。 Is it possible that the similar mistake had been made? 是否有可能出现类似的错误?

The current problem was indeed similar to the former. 目前的问题确实与前者类似。 As it turned out, I should have used the same Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) in Startup.cs:Main 事实证明,我应该在Startup.cs中使用相同的Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) :Main

var host = new WebHostBuilder()
    .UseKestrel()
    .UseConfiguration(config)
    .UseContentRoot(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
    .UseStartup<Startup>()
    .Build();

where by default Directory.GetCurrentDirectory() was used as an argument for UseContentRoot(.) . 默认情况下, Directory.GetCurrentDirectory()用作UseContentRoot(.)的参数。 Also the same operation must be performed several lines of code earlier, when ConfigurationBuilder is called. 当调用ConfigurationBuilder时,也必须在之前的几行代码中执行相同的操作。

The root of the problem is that the windows service is being called from win32 folder, so Directory.GetCurrentDirectory() is giving win32 folder instead of folder of executable file. 问题的根源是从win32文件夹调用windows服务,因此Directory.GetCurrentDirectory()给出了win32文件夹而不是可执行文件的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM