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

柔情痞子 提交于 2019-12-24 03:04:53

问题


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


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




回答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




回答3:


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


来源:https://stackoverflow.com/questions/49483693/net-core-2-0-the-view-index-was-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!