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

查看:23
本文介绍了.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 文件,而是加载了自己的布局文件.如果我尝试删除这个文件,它会抛出找不到布局文件的错误.但是,如果我加载包含在 公共共享项目 中的页面,它会加载自己的 _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 中使用 Razor 类库 (RCL) 重用 UI.NET 核心

请记住,其中 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.NET Core 的 Razor 类库中包含静态资源).

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

更新

利用第三个链接(在 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.

推荐答案

很明显,因为我正在处理 在 ASP.NET Core 中的 Razor 类库中包含静态资源 链接示例保留了默认的 razor 页面类库包含 Area/MyFeatures 文件夹的结构.我修改了它像最后一个链接一样工作 Reuse UI With Razor Class库 (RCL) 在 ASP.NET Core 中,公共共享项目 的结构与 Web 应用程序项目相同(没有 区域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 和 _viewstart.cshtml,否则它将覆盖共享的项目布局.

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

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

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

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