有人使用自定义 ViewMasterPage 吗?
最近,当我构建我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只使用常规内容占位符来表示正文标记 ID 和类。
然后在引用母版页的给定页面中使用它:
I've used just regular content placeholders for body tag id's and classes.
Then use it in the given page that references the master page: