我将.net core 2.0应用程序部署到IIS,我收到以下错误。
InvalidOperationException:视图'索引'没找到。搜索了以下位置:/Views/Home/Index.cshtml /Views/Shared/Index.cshtml
这只是我用来测试Web部署的全新默认.net核心Web模板。我已经尝试了以下代码,但这也没有用。
public static void Main(string[] args)
{
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build()
.Run();
}
是否有一个我要离开的步骤
堆栈追踪:
信息 InvalidOperationException:视图&#39;索引&#39;没找到。搜索了以下位置: /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()
答案 0 :(得分:0)
在我的情况下,我将ASP.Net Core MVC与VS2017结合使用,我将Index.cshtml
的构建操作设置为Content
,但仍然出现错误。
最后,我认为页面有问题,因此我复制了Index.cshtml
的内容,删除了Index.cshtml
,然后重新启动了Visual Studio。然后我Right click on the Folder > Add > View > Give the file name Index
,在添加了新的空白页之后,粘贴复制的内容,然后再次运行,问题就解决了。
答案 1 :(得分:0)
以防万一有人对我的决议感兴趣:
.net核心的IIS插件和我使用的.net核心的版本存在问题。我使用的是.net core的预览版,但没有将.netcore模块更新为正确的版本
答案 2 :(得分:0)
我从.Net Core 2.2迁移到Net Core 3.0时遇到了此错误,Dblock247的回答启发了我检查NuGet软件包。
我发现项目中引用了.Net Core 2.2和.Net Core 3.1软件包。许多.Net Core 2.2软件包没有相应的.Net Core 3.1软件包。删除.Net Core 2.2软件包后,我的应用程序开始工作。
下面是我迁移到.Net Core 3.1时所做的其他更改,以防上述建议对您不起作用。
我还从IWebHost
迁移到IHostBuilder
。这很容易,因为IHostBuild
似乎将IWebHost
包装成Func
。
我的下一个更改是从AddMvc
转到AddControllers
,其中包括调用新的AddNewtonsoft
方法。
services.AddControllers(options =>
{
options.EnableEndpointRouting = false;
})
.AddNewtonsoftJson(options => {
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
最后,在Configure方法中,我添加了新的UseAuthentication
,UsingRouting
,UseEndpoints
和UseAuthorization
:
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
答案 3 :(得分:0)
就我而言,我正在研究Area 所以我没有在控制器中定义Area
添加
[Area("_AreaName_")]
在控制器中的班级解决我的问题之前。