ASP.NET MVC - 带母版页的视图,如何设置标题?
使用母版页时设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在我们的母版页中,我们创建了一个“init”ContentPlaceHolder 和一个“title”ContentPlaceHolder。 如果有人想要以编程方式设置 Page.Title,他们可以在 CSharp 中的 init 占位符中设置它,或者可以使用标签覆盖“标题”占位符。
母版页
查看页面
可以覆盖整个“标题”内容占位符:
或以编程方式设置页面标题。
确保从顶部的 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
View Page
Could either override the entire "title" content placeholder:
or programatically set the page title.
Make sure you remove the Title="" from the Page directive at the top, or you won't be able to programatically change Page.Title.
我看到很多人使用
<%= 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.当我创建一个新的 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"].我最终使用代码隐藏文件在
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 thePage_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)您可以在母版页中执行此操作:
其中 MyTitle = web.config 中的某个值或硬文本(例如“My Web”)
现在在视图页面中(例如索引):
现在,当您浏览主页时,标题将是“我的网站::我的主页”。
You could do this in your Master Page:
where MyTitle = some value from your web.config or hard text like "My Web"
Now in the view pages (Index for example):
Now when you browse your home page, the title would be "My Web :: My Home Page".
我有一个基本视图类,用于从资源文件设置页面标题。 对我来说效果很好。
I have a base view class that sets the page title from a resource file. Works pretty good for me.
我创建了一个名为 Application 的类,具有 Title 属性(使用 get/set):
然后我在每个页面的 Page_Load 事件上设置该属性:
然后我只在母版页上引用该属性:
I created a class called Application with a Title property (using get/set):
I then set the property on the Page_Load event for each page:
then i just reference that property on the master page:
内容页面的 @Page 指令有一个 Title 属性。
There is a Title property of the @Page directive for content pages.
对于 ASP.NET 内容页面,只需在
<%@ %>
占位符中添加Title=""
即可。For ASP.NET content pages just add
Title=""
in the<%@ %>
Placeholder.我们最终进入了
母版页。
这样我们就可以从Page.Title中读取(Page.Title要求head元素存在,否则会抛出异常,用反射器检查)。 然后我们使用我们自己的head元素——MVC方式。
We ended up with
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.
您始终可以在视图页面中使用 javascript:
You could always use javascript in your view page: