ASP.NET Core could not find views

Dmitry Volkov

I have an ASP.NET Core application. The application needs to be started by windows service. 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. 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. Is it possible that the similar mistake had been made?

Dmitry Volkov

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

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(.). Also the same operation must be performed several lines of code earlier, when ConfigurationBuilder is called.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.net Core: The browser could not find the action of the controller

ASP .NET CORE could not find file or assembly with custom assembly

.NET Core Reflection - How to find references of a property inside ASP.NET Core 3 Razor Views?

ASP.NET Core precompiled views on publish

Recursion in ASP.NET Core Razor views

Publish the Views with asp.net core

CLSCompliant attribute for ASP.Net Core views

ASP.NET Core 2.1 File Upload DirectoryNotFoundException: Could not find a part of the path

Could not get to a route in asp.net core

Use glyphicons in razor views in ASP.NET Core 5

ASP net Core Razor Pages - split models for EF and views

Using a DI with partial views in asp.net core razor pages

Determine ASP.NET Core environment name in the views

Asp.Net Core problem with Views and static files (F#)

How to reuse views (pages) in ASP.NET Core MVC?

Inject a service in all ASP.Net Core Razor views

ASP.NET Core MVC Show hide partial views

ASP.Net Core Change Default Views Folder Location

ASP.NET Core - TagHelper, Partial Views and property name conflict

How to get to partial views in ASP.NET Core 3.0

Asp.net core Sakura.AspNetCore.PagedList and partial views

Integration tests with asp.net core (test of controllers without the views)

How to precompile views in ASP.NET Core 2.0?

ASP .NET Core MVC 2.1 mvc Views in plugin

ASP.NET Core populate model only once for all views

ASP.NET MVC CORE - Many to many relations - Create Views

Generating a Controller with views Using Asp.net Core

Prevent Caching Views in ASP.NET Core in VS 19

Azure Web app cannot find views of .NET Core 2.1 App