ASP.NET MVC - 如何设计动态 H1/标题方案?

发布于 2024-09-30 21:22:28 字数 1260 浏览 0 评论 0原文

如果我有一个具有以下视图的 ASP.NET MVC 2 Web 应用程序:

  1. Main.aspx
  2. Page1.aspx
  3. Page2.aspx

并且所有视图都继承从名为 Site.master 的 MasterView 中,

我希望页面能够有一个默认标题/H1,可以在派生视图中覆盖它。

例如,Main.aspx 将具有“MySite - xxx”,Page1.aspx 将具有“MySite - Page 1”,Page2.aspx 将具有“MySite - Page2”。

如果我选择不设置在新的派生视图中的标题/H1,则将显示主标题/H1。

使用 WebForms,我将通过以下方式完成此操作:

  1. 在主代码隐藏上将主代码上的 Title/H1 标记 runat="server" 公开受保护的
  2. 属性 派生页面代码隐藏的 Page_Load,设置属性 (Master.Title = "Page 1")。
  3. 在主后台代码的 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:

  1. Main.aspx
  2. Page1.aspx
  3. 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:

  1. Make the Title/H1 tags on the master runat="server"
  2. Expose protected properties on the master code-behind
  3. On Page_Load of the derived pages code-behind, set the property (Master.Title = "Page 1").
  4. 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 技术交流群。

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

发布评论

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

评论(3

春风十里 2024-10-07 21:22:29

通过快速搜索,我发现了这个:

http ://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx

解释了为什么

<title>MySite<asp:content..../></title>

不起作用

With a quick search I found this:

http://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx

explains why

<title>MySite<asp:content..../></title>

doesn't work

半夏半凉 2024-10-07 21:22:29

我通常是这样做的:

<title>MySite - <%: Page.Title ?? "Default title" %></title>

在您的母版页中。

然后,您可以在内容页面上定义 Title 属性,如下所示:

<%@ Page Language="C#"
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage"
         Title="Page 1"
%>

编辑:

好吧,您可能想看看这个问题:ASP.NET MVC - 带母版页的视图,如何设置标题?

This is how I usually do it:

<title>MySite - <%: Page.Title ?? "Default title" %></title>

in your MasterPage.

Then, you can define the Title property on a content page like this:

<%@ Page Language="C#"
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage"
         Title="Page 1"
%>

Edit:

Well, you might want to see this SO question: ASP.NET MVC - View with master page, how to set title?.

行至春深 2024-10-07 21:22:29

正如您所描述的那样,要容易得多。

只需将内容占位符添加到您的母版页

<title>
    My Site - <asp:ContentPlaceHolder ID="PageTitle" runat="server" />
</title>

然后在您的内容页中使用它作为

<asp:Content ID="Content3" ContentPlaceHolderID="PageTitle" runat="server">
    My Page
</asp:Content>

这种方式可以工作,但您必须使用 HTML HEAD 标记,而不是服务器控件。因此,只需从 HEAD 中删除 runat="server" 即可。

Is much easier as you are describing it.

Just add a content placeholder to your master page

<title>
    My Site - <asp:ContentPlaceHolder ID="PageTitle" runat="server" />
</title>

Then in your Content Page use it as

<asp:Content ID="Content3" ContentPlaceHolderID="PageTitle" runat="server">
    My Page
</asp:Content>

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.

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