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条回答
SAY GOODBYE
2楼-- · 2020-02-05 12:12

I just upgraded from .net core 2.2 to 3. Two ways to resolve I found:

Either call AddRazorRuntimeCompilation() when configuring mvc, like:

services.AddControllersWithViews()
    .AddRazorRuntimeCompilation(...);

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:

<RazorCompileOnBuild>false</RazorCompileOnBuild>
<RazorCompileOnPublish>false</RazorCompileOnPublish>
查看更多
在下西门庆
3楼-- · 2020-02-05 12:12

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 the dotnet 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 this

dotnet myWebFolder/MyIdentityServer.dll

gave me the following output when Identity Server started: enter image description here

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:

enter image description here

Now my content root path is correct and Identity Server finds the Login views at C:\inetpub\aspnetcore\myWebFolder\Views\Account\Login.cshtml

查看更多
我想做一个坏孩纸
4楼-- · 2020-02-05 12:17

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()) from WebHostBuilder in main.

After adding this:

public static void Main(string[] args)
{
    new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>()
        .Build()
        .Run();
}

I then got an exception regarding missing preserveCompilationContext. Once added in project.json my view shows correct.

"buildOptions": {
    "preserveCompilationContext": true,
    "emitEntryPoint": true
},
查看更多
三岁会撩人
5楼-- · 2020-02-05 12:18

In my case, the build action was somehow changed to Embedded resource which is not included in published files.

Changing Embedded resource to Content resolves my issue.

enter image description here

查看更多
Evening l夕情丶
6楼-- · 2020-02-05 12:22

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.

查看更多
倾城 Initia
7楼-- · 2020-02-05 12:25

I have to add in project.json this:

"publishOptions": {
    "include": [
        "wwwroot",
        "**/*.cshtml",
        "appsettings.json",
        "web.config"
    ]
},
查看更多
登录 后发表回答