ASP.NET MVC - 如何设计动态 H1/标题方案?
如果我有一个具有以下视图的 ASP.NET MVC 2 Web 应用程序:
- Main.aspx
- Page1.aspx
- Page2.aspx
并且所有视图都继承从名为 Site.master 的 MasterView 中,
我希望页面能够有一个默认标题/H1,可以在派生视图中覆盖它。
例如,Main.aspx 将具有“MySite - xxx”,Page1.aspx 将具有“MySite - Page 1”,Page2.aspx 将具有“MySite - Page2”。
如果我选择不设置在新的派生视图中的标题/H1,则将显示主标题/H1。
使用 WebForms,我将通过以下方式完成此操作:
- 在主代码隐藏上将主代码上的 Title/H1 标记 runat="server" 公开受保护的
- 属性 派生页面代码隐藏的 Page_Load,设置属性 (Master.Title = "Page 1")。
- 在主后台代码的 Page_PreRender 上,将 Title/H1 标记设置为 "My Site - " + Title;
我们如何使用 ASP.NET MVC 来完成此操作?
我可以在主控中执行此操作:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
然后在视图中设置它:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MySite - Page 1
</asp:Content>
但我希望能够仅在视图中指定“Page 1”,并且标题会神奇地更改为“MySite - Page 1”。知道我的意思吗?标题的“MySite - ”部分需要是标题的模板。
我可能在这里遗漏了一些明显的东西。 :)
If i have an ASP.NET MVC 2 Web Application with the following Views:
- Main.aspx
- Page1.aspx
- Page2.aspx
And all Views inherit from a MasterView called Site.master,
I want to be able to have a default Title/H1 for the page, which can be overriden in the deriving Views.
E.g Main.aspx would have "MySite - xxx", Page1.aspx would have "MySite - Page 1", Page2.aspx would have "MySite - Page2".
And if i choose not to set the Title/H1 in a new derived view, the master Title/H1 would be displayed.
With WebForms, i would accomplish this in the following way:
- Make the Title/H1 tags on the master runat="server"
- Expose protected properties on the master code-behind
- On Page_Load of the derived pages code-behind, set the property (Master.Title = "Page 1").
- On Page_PreRender of the master code-behind, set the Title/H1 tags to "My Site - " + Title;
How can we accomplish this with ASP.NET MVC?
I could do this in the master:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
And then set it in the view:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MySite - Page 1
</asp:Content>
But i wanted to be able to only specify "Page 1" in the View, and the title gets magically changed to "MySite - Page 1". Know what i mean? The "MySite - " part of the title needs to be the template for the title.
I'm probably missing something obvious here. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过快速搜索,我发现了这个:
http ://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx
解释了为什么
不起作用
With a quick search I found this:
http://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx
explains why
doesn't work
我通常是这样做的:
在您的母版页中。
然后,您可以在内容页面上定义
Title
属性,如下所示:编辑:
好吧,您可能想看看这个问题:ASP.NET MVC - 带母版页的视图,如何设置标题?。
This is how I usually do it:
in your MasterPage.
Then, you can define the
Title
property on a content page like this:Edit:
Well, you might want to see this SO question: ASP.NET MVC - View with master page, how to set title?.
正如您所描述的那样,要容易得多。
只需将内容占位符添加到您的母版页
然后在您的内容页中使用它作为
这种方式可以工作,但您必须使用 HTML HEAD 标记,而不是服务器控件。因此,只需从 HEAD 中删除
runat="server"
即可。Is much easier as you are describing it.
Just add a content placeholder to your master page
Then in your Content Page use it as
This way will work but you have to use an HTML HEAD tag, not a server control. So simply remove
runat="server"
from your HEAD.