How do I solve a “view not found” exception in asp.net core mvc project

前端 未结 16 1946
梦毁少年i
梦毁少年i 2020-12-09 15:53

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 oth

相关标签:
16条回答
  • 2020-12-09 16:08

    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>
    
    0 讨论(0)
  • 2020-12-09 16:09

    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:

    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

    0 讨论(0)
  • 2020-12-09 16:11

    In my case the problem was from <EnableDefaultContentItems>false</EnableDefaultContentItems> that I had set in my csproj

    0 讨论(0)
  • 2020-12-09 16:13

    I have to add in project.json this:

    "publishOptions": {
        "include": [
            "wwwroot",
            "**/*.cshtml",
            "appsettings.json",
            "web.config"
        ]
    },
    
    0 讨论(0)
  • 2020-12-09 16:14

    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.

    0 讨论(0)
  • 2020-12-09 16:14

    I had this error on Ubuntu Linux and was trying to run it from the CLI for the first time, so I fixed it by running the dotnet build command with sudo dotnet build, so it turned out to be a permissions issue and the app started fine with dotnet bin/Debug/netcoreapp2.0/MyApp.dll. Sadly the build command didn't notify about the permissions issue.

    0 讨论(0)
提交回复
热议问题