Search code examples
c#asp.net-coreasp.net-core-mvcasp.net-core-2.0identityserver4

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

  • 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.