Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core MVC App run in Visual Studio Code can't find views

I have created a new .Net Core MVC web app in Visual Studio 2015. The IDE is fully updated, and I have the latest .net core SDK installed. It consists of the solution file and multiple project files. I can successfully run it and get to the home page.

I have then opened up the root folder (solution folder) in Visual Studio Code and attempted to run it. It seems to start ok, however it can't find the default view for "Views/Home/Index.cshtml".

I get the following error:

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

The program.cs file is as follows:

var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();

The project.json has the following:

"buildOptions": {
  "emitEntryPoint": true,
  "preserveCompilationContext": true
},

"publishOptions": {
  "include": [
    "wwwroot",
    "Views",
    "Areas/**/Views",
    "appsettings.json",
    "web.config"
  ]
}

I imagine this is due to where it is trying to look for the views, as I am at a solution level, not a project level from within VSC.

Anyone any ideas?

like image 945
eyeballpaul Avatar asked Feb 06 '23 04:02

eyeballpaul


1 Answers

I ran into this same scenario as well:

  1. Opened solution folder from VS Code.
  2. Added the suggested build configuration:

Suggested build config

At this point you can compile and run the project, but it fails in finding the views, as you say.

The solution I've found is to edit the current working directory (cwd property) configuration in .vscode\launch.json:

Default config

And make it to point into the Web project folder:

fixed config

Where the WebProject folder is equivalent to Project root in the previous answer's example.

Hope this helps!

like image 180
Chopin Avatar answered Feb 08 '23 23:02

Chopin