有人使用自定义 ViewMasterPage 吗?

发布于 2024-09-09 00:46:51 字数 1185 浏览 2 评论 0原文

最近,当我构建我的 ASP.NET MVC 应用程序时,我倾向于在我的母版页中始终需要计算、格式化和配置许多项目。其中一些项目包括:

  • 我喜欢将特定的类别附加到正文标记,就像 WordPress 所做的那样来帮助我的 CSS。我通常会附加操作、控制器、页面模板等的名称。
  • 我喜欢在母版页中使用自定义 IIdentity。这个 IIdentity 包括用户的显示名称、用户 ID、用户名等
  • ……取决于项目,

我实现这一目标的方式也已经发展。我首先发送 ViewData 以及每个操作结果来填充母版页中的内容。示例->

// in the action
ViewData["BodyClasses"] = "index home default";
ViewData["UserData"] = userData;

return View();

// in the master page
<% UserData userData = (UserData)ViewData["UserData"] %>
...
<body class="<%= (string)ViewData["BodyClasses"] %>">

那太可怕了。因此,我开始在母版页的顶部放置一些变量,并根据我们必须在母版页中使用的对象填充它们。示例->

<%@ Master Language="C#" Inherits="System.Web.MVC.ViewMasterPage" %>
<% string controller = ViewContext.RouteData.Values["controller"].ToString().ToLower();
   string action = ViewContext.RouteData.Values["action"].ToString().ToLower(); 
   CustomIdentity identity = (CustomIdentity)User.Identity %>

...

<body class="no-js <%= controller + " " + action %>">

这更好,但我觉得必须有一个更简单的解决方案。我开始尝试创建自定义 ViewMasterPage 并添加一些公共属性。是否有其他人在母版页上执行此操作,或者是否有其他我完全缺少的解决方案?

Lately when I've been building my asp.net mvc apps I tend to have a number of items that consistently need to be calculated, formatted and configured in my master pages. Some of these items include:

  • I like to attach specific classed to the body tag like WordPress does to help my CSS out. I usually attach the name of the action, controller, page template, etc.
  • I like to work with a custom IIdentity in my master pages. This IIdentity includes the users nice Display Name, UserID, UserName, etc.
  • etc... depending on the project

The way I've gone about accomplishing this has evolved as well. I started out by sending ViewData along with every action result to populate things in the master page. example->

// in the action
ViewData["BodyClasses"] = "index home default";
ViewData["UserData"] = userData;

return View();

// in the master page
<% UserData userData = (UserData)ViewData["UserData"] %>
...
<body class="<%= (string)ViewData["BodyClasses"] %>">

That was horrible. So I started putting some variables at the top of my master pages and populating them based on the objects we have to work with in the master page. example ->

<%@ Master Language="C#" Inherits="System.Web.MVC.ViewMasterPage" %>
<% string controller = ViewContext.RouteData.Values["controller"].ToString().ToLower();
   string action = ViewContext.RouteData.Values["action"].ToString().ToLower(); 
   CustomIdentity identity = (CustomIdentity)User.Identity %>

...

<body class="no-js <%= controller + " " + action %>">

That's better but I feel like there has to be an easier solution. I started playing around with creating a custom ViewMasterPage and adding some public properties. Is anyone else doing this with the master page or is there another solution to this that I'm completely missing?

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

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

发布评论

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

评论(1

醉生梦死 2024-09-16 00:46:51

我只使用常规内容占位符来表示正文标记 ID 和类。

<body id="<asp:ContentPlaceHolder ID="BodyTagId" runat="server" />">

然后在引用母版页的给定页面中使用它:

<asp:Content ContentPlaceHolderID="BodyTagId" runat="server">about-page</asp:Content>

I've used just regular content placeholders for body tag id's and classes.

<body id="<asp:ContentPlaceHolder ID="BodyTagId" runat="server" />">

Then use it in the given page that references the master page:

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