Share same _layout.cshtml within different project

2019-06-24 16:03发布

问题:

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

Exception Stack:

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

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.

回答1:

I found that the linked .cshtml files do get copied when the site is built but they are copied to the build bin folder. In my Startup.cs I added this extra area route:-

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

your path may differ but if you open up the solution you should be able to adapt this answer to work.

I didn't need this step when publishing to the production web server running IIS using WebPublishMethod MSDeploy.



回答2:

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>