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 ?
Check your .csproj file. Make sure your file(s) are not listed like:
If it is listed like above, remove the ones you need. Sometimes, that happens when we do right click and change build setting to Compile.
FWIW, In our case we received this error when accessing
localhost:<port>/graphql
even though theView/GraphQL/Index.cshtml
was in the right place. We got the error becausewwwroot/bundle.js
andwwwroot/style.js
was mistakenly not initially committed, since our.gitignore
includedwwwroot
. Discovered it by inspecting the network traffic and seeing it was these files giving the 404 and not theIndex.cshtml
itself.Visual Code With dotnet version 2.2.300 gave an error message, so not a runtime exception.
My controller was derived from ControlerBase instead of Controller. So, I had to change ControllerBase to Controller.
Im my case it simply was a "forget" error...
I had the folder with view not named the same as the controller name.
Renamed the folder to the correct name.. and works...
Reply to @LukaszDev (dont think I can attach images to a comment) My view is Index.cshtml, typo from my side. See attached screenshot
My controller is as simple as can be. I get same error for both Index and Welcome
I am using VS2017. In my case there was the view file at the proper place (with the proper name). Then I remembered that I renamed it before, (and the VS somehow did not asked me if I wanna change all the references as usual). So I finally deleted the view, then I made it again, and this solved my problem.