'Self hosted ASp.net core 2.2 app, The view 'Index' was not found. The following locations were searched

I am creating ASP.NET Core web application and hosting it via console.

I am using IdentitySvr4 Quick Start UI and getting all the files to my project.

Console Application

Now when I start the server by running console and browse to http://localhost:44322/, I am getting error,

InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml

I have created this project as console application and added all the nugets along the way.

The webserver starts and listens on the port, but somehow the view engine does not understand.

Main Program:

class Program
{
        static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args).UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://*:44322")
            .UseStartup<Startup>();
}

Startup class:

public class Startup
{

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var signingCertificate = new X509Certificate2(Helpers.GetCertificate());
            services.AddIdentityServer()
                .AddSigningCredential(new X509Certificate2(signingCertificate))
                .AddTestUsers(InMemoryConfiguration.Users().ToList())
                .AddInMemoryClients(InMemoryConfiguration.Clients().ToList())
                  .AddInMemoryApiResources(InMemoryConfiguration.ApiResources());

            services.AddMvc();


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            app.UseDeveloperExceptionPage();

            app.UseIdentityServer();

            app.UseStaticFiles();

            app.UseMvcWithDefaultRoute();

        }
}


Solution 1:[1]

Normally I would not answer my question, and just add a comment , but I think this will save the devs a lot of effort and pain.

It was simple,

I edited the .csproj with Notepad++ and found following settings:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IdentityServer4" Version="2.3.2" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
  </ItemGroup>

</Project>

After refering to a webproj, just changed it to a webproj by changing the root from "Microsoft.NET.Sdk" , to "Microsoft.NET.Sdk.Web"

 <Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IdentityServer4" Version="2.3.2" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
  </ItemGroup>

</Project>

And tha's it. Not sure why all the application logic will depend on the project type (since it could be hosted any where), but works like a charm.

Solution 2:[2]

For me, the error was within my .csproj file. The ItemGroup content was excluding Index.cshtml. I removed this reference and now I can navigate to Index.cshtml.

<pre><code>
<ItemGroup>
<Content Remove="Views\Employee\Delete.cshtml" />
<Content Remove="Views\Employee\Index.cshtml" />
</ItemGroup>
</code></pre>

Solution 3:[3]

I edited the .csproj

Just remove this (if is Exist)

"<RazorCompileOnBuild>false</RazorCompileOnBuild>"
"<RazorCompileOnPublish>false</RazorCompileOnPublish>"

from <PropertyGroup>

By default the "RazorCompileOnBuild" & "RazorCompileOnPublish" not found in <PropertyGroup>

Solution 4:[4]

dotnet clean

fixed it for me (.NETCore 6)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 carltonstith
Solution 3 Mohammad Dahdooh
Solution 4 Michael Ribbons