gpt4 book ai didi

asp.net-mvc - 使用 ASP.NET Core 2.0 MVC 在一个解决方案的不同项目中共享相同的 _layout.cshtml

转载 作者:行者123 更新时间:2023-12-02 00:08:55 24 4
gpt4 key购买 nike

我们正在开发多个具有不同功能的内联网网站。我们计划有一个根项目(具有一些基本功能),用户可以从中导航到不同的其他项目。我们计划所有此类项目都应使用相同的布局_Layout.cshtml。为了实现这一点,我们尝试将“副项目”中的 _Layout.cshtml 链接到根项目。我们使用了 VS 内置链接方法,如下所述:https://stackoverflow.com/a/19862471/9641435该文件链接到路径 /Views/Shared/_Layout.cshtml,没有任何错误消息。但是,如果我们启动站点项目之一,则会出现以下错误消息:

An unhandled exception occurred while processing the request.

InvalidOperationException: The layout view '_Layout' could not be located. The following locations were searched: /Views/Home/_Layout.cshtml /Views/Shared/_Layout.cshtml

Microsoft.AspNetCore.Mvc.Razor.RazorView.GetLayoutPage(ViewContext context, string executingFilePath, string layoutPath)

异常堆栈:

InvalidOperationException: The layout view '_Layout' could not be located. The following locations were searched: /Views/Home/_Layout.cshtml /Views/Shared/_Layout.cshtml

Microsoft.AspNetCore.Mvc.Razor.RazorView.GetLayoutPage(ViewContext context, string executingFilePath, string layoutPath)

Microsoft.AspNetCore.Mvc.Razor.RazorView+d__18.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.Razor.RazorView+d__14.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+d__22.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+d__21.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.ViewResult+d__26.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__19.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+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+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+d__17.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__15.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Builder.RouterMiddleware+d__4.MoveNext()

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()

我们尝试使用此解决方案修复它:https://stackoverflow.com/a/24079584/9641435但插入该代码并没有改变任何东西。

我们正在寻找一种解决方案来解决所描述的问题,或者可能是一种更好的方法来解决整个问题。

最佳答案

我发现链接的 .cshtml 文件在构建网站时确实被复制,但它们被复制到构建 bin 文件夹中。在我的 Startup.cs 中,我添加了这个额外的区域路线:-

if (Env.IsDevelopment())
{
services.Configure<Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions>(options =>
{
options.AreaViewLocationFormats.Add("/bin/Debug/netcoreapp2.1/Areas/{2}/Views/{1}/{0}.cshtml");
});
}

您的路径可能会有所不同,但如果您打开解决方案,您应该能够调整此答案以使其工作。

使用 WebPublishMethod MSDeploy 发布到运行 IIS 的生产 Web 服务器时,不需要此步骤。

关于asp.net-mvc - 使用 ASP.NET Core 2.0 MVC 在一个解决方案的不同项目中共享相同的 _layout.cshtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com