与 razor(cshtml) 共享模板 site.master aspx(c#)

发布于 2024-12-26 17:34:50 字数 153 浏览 2 评论 0原文

我已将项目升级到 mvc3,并希望保留所有页面并向其中添加新页面。 现在我的问题是,如果我开始使用 razor,如何在 view.aspx(C#) 和 razor 视图(cshtml) 之间共享我的 site.master 模板页面。 这是因为我不想让我的母版页及其代码由于明显的原因而重复。

I have upgraded my project to mvc3 and want to keep all my pages and add new into it.
Now my question is if i start using razor, how can share my site.master template page between view.aspx(C#) and razor views(cshtml).
This is because i do not want to have my master page and its code duplicated for obvious reasons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2025-01-02 17:34:50

Scott Hanselman 对此发表了博客

最常见的情况是某人拥有现有的 WebForms
(ASPX) 母版页运行良好,他们现在想要包含一个
他们的应用程序中很少有 Razor 页面,但不想维护两个
实际上相同的母版页(一个用于 ASPX,一个用于 Razor)。他们
想要与 WebForms 和 Razor 共享他们的 WebForms Master
视图。

他在博客文章中提供了解决方案。

Scott Hanselman blogged about this:

Most commonly the scenario is that someone has an existing WebForms
(ASPX) Master Page that works nicely, and they now want to include a
few Razor pages in their application but don't want to maintain two
effectively identical Master Pages (one for ASPX, one for Razor). They
want to share their WebForms Master with both WebForms and Razor
Views.

And he provides the solution in his blog post.

帥小哥 2025-01-02 17:34:50

您不能将母版页 (.master) 与 razor 视图 (.cshtml) 一起使用。
它们不兼容,剃刀视图需要布局页面才能实现母版页提供的功能。

您可以创建一个与当前主页具有相同标记的布局页面。这实际上不会是重复,因为它们不兼容。

或者更好的是,您可以将所有内容都转换为剃须刀,有一些工具可以帮助自动化该过程。查看 telerik razor 转换器 提供的免费工具

You cannot use Master pages (.master) with razor views (.cshtml).
They are not compatible, razor views require a layout page to achieve what master pages offer.

You could create a layout page that has the same markup with the current master page. This will not really be duplication since they are not compatible.

Or better still you could just convert everything to razor, there are tools available to help automate the process. Check out this free tool by telerik razor converter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文