Dblock247
Dblock247

Reputation: 6725

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


Upvotes: 3

Views: 18036

Answers (6)

nammadhu
nammadhu

Reputation: 353

i was also facing similar issue,took long time to fix... Solution is in .csproj if any views are with Remove then delete those entry and include those views to project in solution explorer... then actual fix is just add one test view viewfile index3.cstml and make it called from action then deploy all works well. Later can remove that extra view file. 100% it works.

Upvotes: 0

Tropin Alexey
Tropin Alexey

Reputation: 766

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>

Upvotes: 0

Purnima Bhatia
Purnima Bhatia

Reputation: 67

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.

Upvotes: 1

Chuck Conway
Chuck Conway

Reputation: 16435

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();
        });

Upvotes: 1

Dblock247
Dblock247

Reputation: 6725

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

Upvotes: 1

yu yang Jian
yu yang Jian

Reputation: 7171

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.

Upvotes: 5

Related Questions