I'm trying to create a ASP.NET Core MVC test app running on OSX using VS Code. I'm getting a 'view not found' exception when accessing the default Home/index (or any other views I tried).
This is the Startup configuration
public void Configure(IApplicationBuilder app) {
// use for development
app.UseDeveloperExceptionPage();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc( routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}"
);
});
}
And I have the view defined in Views/Home/index.cshtml
, and I have the following packages included on project.json
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0-rc2-3002702",
"type": "platform"
},
"Microsoft.AspNetCore.Razor.Tools" : "1.0.0-preview1-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Routing": "1.0.0-rc2-final"
},
And finally, this is the exception I get.
System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
at Microsoft.AspNetCore.Mvc.ViewResult.<ExecuteResultAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown --- ...
Any suggestions of what I might be missing ?
I just upgraded from .net core 2.2 to 3. Two ways to resolve I found:
Either call
AddRazorRuntimeCompilation()
when configuring mvc, like:more info here: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#opt-in-to-runtime-compilation
Or remove the following lines from the .csproj:
I had this same issue while building an Identity Server instance, everything worked great if I ran the project from Visual Studio but after publishing the project and running with the
dotnet
command I got the "View not found" error when Identity Server tried to serve up the Login view. I already had verified everything else mentioned here but it still wasn't working. I finally found that the problem was with how I Was running thedotnet
command. I was running it from the parent folder, one level above my Identity Server instance and this affected the content root path, for example, running thisgave me the following output when Identity Server started:
So, the full path of my dll in this case is C:\inetpub\aspnetcore\myWebFolder\MyIdentityServer.dll and I ran the
dotnet
command from the C:\inetpub\aspnetcore\ folder.To get the correct content root I had to make sure I ran the
dotnet
command from the same folder where the dll is, as in 'dotnet MyIdentityServer.dll`. That change gave me this output:Now my content root path is correct and Identity Server finds the Login views at C:\inetpub\aspnetcore\myWebFolder\Views\Account\Login.cshtml
I found this missing piece. I ended up creating a ASP.NET Core project in VS2015 and then compare for differences. It turns out I was missing
.UseContentRoot(Directory.GetCurrentDirectory())
fromWebHostBuilder
in main.After adding this:
I then got an exception regarding missing
preserveCompilationContext
. Once added in project.json my view shows correct.In my case, the build action was somehow changed to Embedded resource which is not included in published files.
Changing
Embedded resource
toContent
resolves my issue.For anyone struggling with this, I had a different problem than all the above. My local version worked fine, but the live production version gave this error. It turned out the Unix filenames were different from the ones showing in Windows.
Git has a nasty setting that's enabled by default: core.ignorecasegit ( you can check this setting on the commandline with
config --get core.ignorecase
)The solution was to rename the files to something else (e.g. xxx.cs ), commit and push, then rename them back to original with the correct casing/capitalization and commit and push again.
I have to add in project.json this: