Reputation: 1735
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?
Upvotes: 4
Views: 3191
Reputation: 60
In my case, all I had to do is go to my project path and type dotnet build
to generate the path of my new added views into project.razor.json
file. I think in Visual Studio, its generated automatically because we do the thing using an UI.
Upvotes: 0
Reputation: 1452
I ran into this same scenario as well:
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
:
And make it to point into the Web project folder:
Where the WebProject
folder is equivalent to Project root
in the previous answer's example.
Hope this helps!
Upvotes: 8
Reputation: 1889
.Net core projects have different default routing path then it was with ASP.NET 4.5 (MVC5) projects.
Lets say that your default route in Startup.cs file is this:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
You want your user to access a view that is Index action for Home controller, e.g. http://mysite/Home/Index
Your directory structure would need to be
In case this is not working for you, check the startup file and see if your default route matches the one from my example.
Upvotes: 0