ASP.NET MVC - 带母版页的视图,如何设置标题?

发布于 2024-07-09 06:20:03 字数 190 浏览 5 评论 0原文

使用母版页时设置 html 标题(在 head 中)以供查看的首选方法是什么?

一种方法是在 .aspx 文件中使用 Page.Title,但这需要在母版页中使用,这可能会扰乱 HTML 代码。 因此,我们假设没有服务器端控件,只有纯 html。 还有更好的想法吗?

更新:我想在视图中而不是在控制器或模型中设置标题。

What is prefered way of setting html title (in head) for view when using master pages?

One way is by using Page.Title in .aspx file, but that requires in master page which can mess with HTML code. So, lets assume no server side controls, only pure html. Any better ideas?

UPDATE: I would like to set title in view NOT in the controller or model.

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

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

发布评论

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

评论(11

浮生未歇 2024-07-16 06:20:03

在我们的母版页中,我们创建了一个“init”ContentPlaceHolder 和一个“title”ContentPlaceHolder。 如果有人想要以编程方式设置 Page.Title,他们可以在 CSharp 中的 init 占位符中设置它,或者可以使用标签覆盖“标题”占位符。

母版页

<asp:ContentPlaceHolder id="init" runat="server"></asp:ContentPlaceHolder>
<head runat="server">    
    <asp:ContentPlaceHolder ID="title" runat="server">
        <title><%=this.Page.Title%></title>
    </asp:ContentPlaceHolder>
</head>

查看页面
可以覆盖整个“标题”内容占位符:

<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
       <title>Home Page</title>
</asp:Content>

或以编程方式设置页面标题。

<asp:Content ID="Content1" ContentPlaceHolderID="init" runat="server">
    <%this.Title = "Home Page";%>
</asp:Content>

确保从顶部的 Page 指令中删除 Title="",否则您将无法以编程方式更改 Page.Title。

In our master pages, we created both an "init" ContentPlaceHolder, and a "title" ContentPlaceHolder. If someone wants to programatically set Page.Title, they can set it in CSharp in the init placeholder, or they can override the "title" placeholder using tags.

Master Page

<asp:ContentPlaceHolder id="init" runat="server"></asp:ContentPlaceHolder>
<head runat="server">    
    <asp:ContentPlaceHolder ID="title" runat="server">
        <title><%=this.Page.Title%></title>
    </asp:ContentPlaceHolder>
</head>

View Page
Could either override the entire "title" content placeholder:

<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
       <title>Home Page</title>
</asp:Content>

or programatically set the page title.

<asp:Content ID="Content1" ContentPlaceHolderID="init" runat="server">
    <%this.Title = "Home Page";%>
</asp:Content>

Make sure you remove the Title="" from the Page directive at the top, or you won't be able to programatically change Page.Title.

一个人的旅程 2024-07-16 06:20:03

我看到很多人使用 <%= ViewData["Title"] %> 选项。

我想您还可以插入一个名为 Title 的 ContentPlaceHolder ,然后在您的页面上使用它,但在我见过的所有 MVC 示例中,他们都使用第一个选项。

I see a lot of people that use the <%= ViewData["Title"] %> option.

I suppose you could also insert a ContentPlaceHolder named Title and then just use that on your page, but in all the MVC examples I've seen, they use the first option.

不回头走下去 2024-07-16 06:20:03

当我创建一个新的 MVC 项目时,它包含文件并使用母版页。 看看它似乎将标题作为 ViewData["Title"] 传递给 ViewData,并且在母版页中,在 中有一个输出 ViewData["Title" 的脚本块]。

When I create a new MVC project it has files in there and uses a master page. Looking at that it seems it passes the title to the ViewData as ViewData["Title"] and in the master page, in the <head> there is a script block that outputs ViewData["Title"].

街道布景 2024-07-16 06:20:03

我最终使用代码隐藏文件在 Page_Load() 方法中实现 Page.Title="..."

我不喜欢这样做,但是尝试直接在 .aspx 页面中实现更改并不起作用,因为它导致存在两个 </code> 标记,即我生成的标记,以及由页面继承的主文件生成的文件。

理想情况下,我的页面代码应该覆盖主文件的 </code> 值,但我想这只是 ASP.Net MVC 仍然存在的怪癖之一,并且可能已经在较新版本的 ASP.Net MVC 框架(我们仍处于 ASP.Net MVC Beta 版本)

I ended up using a code-behind file to implement Page.Title="..." in the Page_Load() method.

I didn't like doing this, however attempts to implement the change directly in the .aspx page did not work, as it resulted in two <title> tags being present, the one I generated, and the one generated by the Master file the page inherited from.

Ideally, my page code should have overridden the master file's <title> value, but I guess this is just one of those quirks that ASP.Net MVC still has, and one that may already be fixed in a newer version of the ASP.Net MVC Framework (we're still on ASP.Net MVC Beta)

好菇凉咱不稀罕他 2024-07-16 06:20:03

您可以在母版页中执行此操作:

<title>
    <%:MyTitle + " :: " %>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server">
    </asp:ContentPlaceHolder>
</title>

其中 MyTitle = web.config 中的某个值或硬文本(例如“My Web”)

现在在视图页面中(例如索引):

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%:"My Home Page"%>

现在,当您浏览主页时,标题将是“我的网站::我的主页”。

You could do this in your Master Page:

<title>
    <%:MyTitle + " :: " %>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server">
    </asp:ContentPlaceHolder>
</title>

where MyTitle = some value from your web.config or hard text like "My Web"

Now in the view pages (Index for example):

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%:"My Home Page"%>

Now when you browse your home page, the title would be "My Web :: My Home Page".

不甘平庸 2024-07-16 06:20:03

我有一个基本视图类,用于从资源文件设置页面标题。 对我来说效果很好。

I have a base view class that sets the page title from a resource file. Works pretty good for me.

温柔戏命师 2024-07-16 06:20:03

我创建了一个名为 Application 的类,具有 Title 属性(使用 get/set):

public static class Application
{
    static string title;

    public static string Title
    {
        get { return title; }
        set { title = value; }
    }
}

然后我在每个页面的 Page_Load 事件上设置该属性:

Application.Title = "Silly Application";

然后我只在母版页上引用该属性:

<div id="divApplicationTitle">
     <asp:HyperLink runat="server" NavigateUrl="~/Default.aspx"><asp:Image ID="imgApplicationImage" runat="server" SkinID="skinApplicationLogo" /><%=Application.Title%></asp:HyperLink> 
</div> 

I created a class called Application with a Title property (using get/set):

public static class Application
{
    static string title;

    public static string Title
    {
        get { return title; }
        set { title = value; }
    }
}

I then set the property on the Page_Load event for each page:

Application.Title = "Silly Application";

then i just reference that property on the master page:

<div id="divApplicationTitle">
     <asp:HyperLink runat="server" NavigateUrl="~/Default.aspx"><asp:Image ID="imgApplicationImage" runat="server" SkinID="skinApplicationLogo" /><%=Application.Title%></asp:HyperLink> 
</div> 
眼眸 2024-07-16 06:20:03

内容页面的 @Page 指令有一个 Title 属性。

There is a Title property of the @Page directive for content pages.

拍不死你 2024-07-16 06:20:03

对于 ASP.NET 内容页面,只需在 <%@ %> 占位符中添加 Title="" 即可。

For ASP.NET content pages just add Title="" in the <%@ %> Placeholder.

╰◇生如夏花灿烂 2024-07-16 06:20:03

我们最终进入了

<head runat=server visible=false>

母版页。

这样我们就可以从Page.Title中读取(Page.Title要求head元素存在,否则会抛出异常,用反射器检查)。 然后我们使用我们自己的head元素——MVC方式。

We ended up with

<head runat=server visible=false>

in master page.

This way we can read from Page.Title (Page.Title requires head element to exist, otherwise it throws an exception, checked that with reflector). We then use our own head element - MVC way.

红墙和绿瓦 2024-07-16 06:20:03

您始终可以在视图页面中使用 javascript:

<script type="text/javascript>
    document.title = "Hello World";
</script>

You could always use javascript in your view page:

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