.net core 2.1在各个项目中重用_Layout [英] .net core 2.1 reusing _Layout throughout various projects

查看:319
本文介绍了.net core 2.1在各个项目中重用_Layout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多个.net core 2.1 Razor Page项目中重用相同的_Layout.cshtml文件。目前,我有一个Razor页面类库(公共共享项目),该库是_Layout.cshtml文件所在的位置。除了布局文件外,还有其他所有项目也应该可以访问的css和js文件(我能够完成这项工作)。

I am trying to reuse the same _Layout.cshtml file, throughout multiple .net core 2.1 Razor Page projects. Currently I have a Razor Page Class Library (Common shared project) that is where the _Layout.cshtml file will be located. Along with the layout file there are css and js files that should also be accessible to all other projects (I was able to make this work).

第二个项目将具有引用到共同共享项目,并且应该能够利用其中的_Layout.cshtml文件。

secondary projects will have references to the Common shared project and should be able to utilize the _Layout.cshtml file from it.

我的问题是当前未加载当我从这些项目访问页面时,使用通用的_layout文件,而是加载其自己的布局文件。如果我尝试删除此文件,则会引发错误,即找不到布局文件。但是,如果我加载 Common共享项目中包含的页面,它将加载它自己的_layout。

My issue is that currently it is not loading the common _layout file when I am accessing pages from these projects, instead it loads its own layout file. If I try deleting this file it throws the error that it cannot find the layout file. However, if I load a page contained inside of the Common shared project it does load its own _layout.

我使用了以下教程来得到我所在的位置,但我不知道如何使辅助项目加载共享项目中的布局。

I have used the following tutorials to get where I am at but I could not figure out how to make the secondary projects load the layout in the Common shared project.

在其中包含静态资源ASP.NET Core中的Razor类库

Razor类库也可以打包静态文件(js,css等)

在ASP.NET Core中与Razor类库(RCL)一起使用UI

请记住,第2个这些文章是基于共享css和js文件(确实起作用)的,但是我也阅读了布局的其他示例,也没有运气(当前我没有其他文章的链接)

Please keep in mind that 2 of these articles are based on sharing the css and js files (which did work) but I have read up on other examples for the layout as well and have had no luck (currently I don't have the links to those other articles)

就我编写的代码而言,我目前正在尝试一个示例项目,它与第一篇文章相同(在ASP中的Razor类库中包含静态资源.NET Core )。

As far as what I have coded I'm currently experimenting on a sample project and its the same as the first article (Including Static Resources In Razor Class Libraries In ASP.NET Core).

更新

使用第3个链接(在ASP.NET Core中与Razor类库(RCL)一起使用UI)能够实现我的目标。但是,我以前使用的是相同的策略,但无法正常工作。我将尝试更多地玩这个游戏。如果我还有其他更新,我会将其发布。

Utilizing the 3rd link (Reuse UI With Razor Class Libraries (RCL) In ASP.NET Core) I was able to accomplish my goal. However, I was using this same strategy previously and it was not working. I am going to try playing around with this a bit more. If I have any other updates I will post them.

推荐答案

显然,由于我正在使用在Razor类库中包含静态资源在ASP.NET Core 链接中,示例保留了包含 Area / MyFeatures 文件夹的默认剃刀页面类库结构。我对其进行了修改,使其像最后一个链接一样工作与Razor类一起使用UI ASP.NET Core中的库(RCL),其中公共共享项目的结构与Web应用程序项目的结构相同(没有 Area MyFeatures 文件夹)。我还添加了:

So apparently since I was working off of the Including Static Resources In Razor Class Libraries In ASP.NET Core link the example left the default razor page class library structure containing the Area/MyFeatures folders. I modified this to work like the last link Reuse UI With Razor Class Libraries (RCL) In ASP.NET Core where the Common shared project is structured the same way that the web application project is (no Area or MyFeatures folder). I also added:

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers

到位于共同共享项目的_ViewStart.cshtml的末尾。

to the end of my _ViewStart.cshtml located in the Common shared project.

注意,您必须从Web应用程序中删除_layout.cshtml,_viewimports.cshtml和_viewstart.cshtml,否则它将覆盖共享的项目布局。

无论如何,这似乎已经解决了该问题,并且现在可以正常工作。

Anyhow this seems to have fixed the issue and it is now working.

这篇关于.net core 2.1在各个项目中重用_Layout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆