How do I solve a “view not found” exception in asp

2020-02-05 11:50发布

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 ?

12条回答
手持菜刀,她持情操
2楼-- · 2020-02-05 12:26

Check your .csproj file. Make sure your file(s) are not listed like:

<ItemGroup>
   <Content Remove="Views\Extractor\Insert.cshtml" />
   <Content Remove="Views\_ViewImports.cshtml" />
</ItemGroup>

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.

查看更多
走好不送
3楼-- · 2020-02-05 12:30

FWIW, In our case we received this error when accessing localhost:<port>/graphql even though the View/GraphQL/Index.cshtml was in the right place. We got the error because wwwroot/bundle.js and wwwroot/style.js was mistakenly not initially committed, since our .gitignore included wwwroot. Discovered it by inspecting the network traffic and seeing it was these files giving the 404 and not the Index.cshtml itself.

查看更多
来,给爷笑一个
4楼-- · 2020-02-05 12:32

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.

查看更多
趁早两清
5楼-- · 2020-02-05 12:32

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

查看更多
干净又极端
6楼-- · 2020-02-05 12:36

Reply to @LukaszDev (dont think I can attach images to a comment) My view is Index.cshtml, typo from my side. See attached screenshot

enter image description here

My controller is as simple as can be. I get same error for both Index and Welcome

using Microsoft.AspNetCore.Mvc;

namespace app1 {

    [Route("[controller]")]
    public class HomeController : Controller {

        // default action, configured in Startup.cs route
        public IActionResult Index() {
            // handles route /home
            return View();
        }

        [Route("welcome")]
        public IActionResult WelcomeActionDoesNotHaveToMatchName() {
            // handles router /home/welcome
            return View("Welcome"); 
        }
    }
}
查看更多
放我归山
7楼-- · 2020-02-05 12:39

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.

查看更多
登录 后发表回答