'.Net Core 2.0 The view 'Index' was not found

I deployed .net core 2.0 application to IIS and I get the following error.

InvalidOperationException: The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml

This is just a brand new default .net core web template that I am using to test web deploy. I have tried the below code but that doesn't work either.

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

Is there a step that I am leaving out

Stack Trace:

Message InvalidOperationException: The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations)

InvalidOperationException: The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml
    Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable<string> originalLocations)

    Microsoft.AspNetCore.Mvc.ViewResult+<ExecuteResultAsync>d__26.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeResultAsync>d__19.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResultFilterAsync>d__24.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

    Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()

    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()



Solution 1:[1]

In my case I'm using ASP.Net Core MVC with VS2017, I have set the Build Action of Index.cshtml to Content but still got error.

Finally I guess there's something wrong in the page, so I copy the content of Index.cshtml, delete Index.cshtml and restart Visual Studio. Then I Right click on the Folder > Add > View > Give the file name Index, after adding the new empty page, paste the copied content, and run again, the problem is solved.

Solution 2:[2]

Just in case anyone is interested in my resolution:

It was issue with the IIS Plugin for .net core and the version of .net core I was using. I was using a preview build of .net core and I didn't update .netcore module to the correct version

Solution 3:[3]

In my case I was working on Area So I have not defined Area in my Controller

Adding

    [Area("_AreaName_")]

before Class in Controller resolved my Issue.

Solution 4:[4]

I encountered this error while migrating from .Net Core 2.2 to Net Core 3.0, Dblock247's answer inspired me to check the NuGet packages.

I found both .Net Core 2.2 and .Net Core 3.1 packages referenced in the project. Many of the .Net Core 2.2 packages don't have corresponding .Net Core 3.1 packages. Once I removed the .Net Core 2.2 packages my application started working.

Below are other changes I made while migrating to .Net Core 3.1, in case the above suggestion doesn't work for you.

I also migrated from IWebHost to IHostBuilder. It's fairly easy because IHostBuildseems to wrap the IWebHost into a Func.

The next change I made was to move from AddMvc to AddControllers, which includes calling the new AddNewtonsoft method.

        services.AddControllers(options =>
        {
            options.EnableEndpointRouting = false;

        })
        .AddNewtonsoftJson(options => {
            options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        });

Lastly, in the Configure method, I added the new UseAuthentication, UsingRouting, UseEndpoints, and UseAuthorization:

        app.UseAuthentication();
        app.UseRouting();
        app.UseAuthorization();


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();

            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapRazorPages();
        });

Solution 5:[5]

In my case it was this option. After setting it up from "Do not copy" value to any option of "Copy..." everything works as expected. The screenshot is from JetBrains Rider, this is properties of my Index.cshtml

enter image description here

This action adds this ItemGroup into project file:

<Project>
...
    <ItemGroup>
        <Content Update="Views\Home\Index.cshtml">    
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
...
    </ItemGroup>
</Project>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yu yang Jian
Solution 2 Dblock247
Solution 3 Purnima Bhatia
Solution 4 Chuck Conway
Solution 5