使用ASP.NET Core 2.0 MVC在一个解决方案的不同项目中共享相同的_layout.cshtml [英] Share same _layout.cshtml within different projects of one solution with ASP.NET Core 2.0 MVC
问题描述
我们正在开发具有不同功能的多个Intranet网站.我们计划有一个根项目(具有一些基本功能),用户可以从中导航到其他项目.
我们计划所有此类项目都应使用相同的布局_Layout.cshtml
.
为此,我们尝试将_Layout.cshtml
从"side-projects"链接到根项目.我们使用了VS内置链接方法,如下所述: https://stackoverflow.com/a/19862471/9641435
该文件已链接到路径/Views/Shared/_Layout.cshtml
,没有任何错误消息.但是,如果我们启动一个站点项目,则会出现以下错误消息:
We are developing multiple intranet websites with different functionalities. We plan to have a root project (with some basic functionality) from which the user can navigate to the different other projects.
We plan that all projects of this kind should use the same Layout _Layout.cshtml
.
To accomplish this we tried to link the _Layout.cshtml
from the "side-projects" to the root project. We used the VS buildin link method as described here: https://stackoverflow.com/a/19862471/9641435
The file is linked without any error-message to the path /Views/Shared/_Layout.cshtml
. However if we start one of the site-projects the following error message appears:
处理请求时发生未处理的异常.
An unhandled exception occurred while processing the request.
InvalidOperationException:布局视图"_Layout"不能为 位于.搜索了以下位置: /Views/Home/_Layout.cshtml/Views/Shared/_Layout.cshtml
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 上下文,字符串executeFilePath,字符串layoutPath)
Microsoft.AspNetCore.Mvc.Razor.RazorView.GetLayoutPage(ViewContext context, string executingFilePath, string layoutPath)
异常堆栈:
InvalidOperationException:布局视图"_Layout"不能为 位于.搜索了以下位置: /Views/Home/_Layout.cshtml/Views/Shared/_Layout.cshtml
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 上下文,字符串executeFilePath,字符串layoutPath)
Microsoft.AspNetCore.Mvc.Razor.RazorView.GetLayoutPage(ViewContext context, string executingFilePath, string layoutPath)
Microsoft.AspNetCore.Mvc.Razor.RazorView + d__18.MoveNext()
Microsoft.AspNetCore.Mvc.Razor.RazorView+d__18.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Razor.RazorView + d__14.MoveNext()
Microsoft.AspNetCore.Mvc.Razor.RazorView+d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor + d__22.MoveNext()
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor + d__21.MoveNext()
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor+d__21.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.ViewResult + d__26.MoveNext()
Microsoft.AspNetCore.Mvc.ViewResult+d__26.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker + d__19.MoveNext()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__19.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker + d__24.MoveNext()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__24.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext 上下文)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(下一个参考状态, ref作用域范围,ref对象状态,ref bool isCompleted)
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()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(下一个参考状态, ref作用域范围,ref对象状态,ref bool isCompleted)
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()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker + d__15.MoveNext()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware + d__4.MoveNext()
Microsoft.AspNetCore.Builder.RouterMiddleware+d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware + d__7.MoveNext()
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()
我们尝试使用以下解决方案对其进行修复: https://stackoverflow.com/a/24079584/9641435 但插入该代码不会更改任何内容.
We tried to fix it with this solution: https://stackoverflow.com/a/24079584/9641435 but inserting that code did not change anything.
我们正在寻找解决上述问题的解决方案,或者寻求解决整体问题的更好方法.
We are searching for a solution to fix the described problem or maybe a better aproach to tackle the overall problem.
推荐答案
我认为您不必让视图复制到输出目录,而只需将build属性设置为content,得到以下代码在您的csproj文件中:
I think you don't have to let the view get copied to the output directory but just have to set the build property to content, resulting in the following code in your csproj file:
<ItemGroup>
<Content Include="..\shared\path\to\_Layout.cshtml" Link="Views/Shared/_Layout.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="..\shared\path\to\_Layout.cshtml" Link="Views/Shared/_Layout.cshtml" />
</ItemGroup>
这篇关于使用ASP.NET Core 2.0 MVC在一个解决方案的不同项目中共享相同的_layout.cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!