ASP.NET MVC - View with master page, how to set title?
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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"].
Solution 5
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)
bh213
Updated on May 14, 2020Comments
-
bh213 about 4 years
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.
-
bh213 over 15 yearsYes, but I said above, it requires <head runat=server> in master page, which is not what I want.
-
Bender over 15 yearsYour question is nonsensical then, because you're saying you want to set the title of your content page using server-side constructs, but you don't want to use server-side constructs.
-
bh213 over 15 yearsWell, I would like to avoid any aspx controls. <head runat=server> eats some of the html we use, which we could solve, but I would prefer to use only pure html. Master pages and controls should be acceptable though.
-
Timothy Khouri over 15 yearsIf you're using MVC... you really shouldn't be relying on "runat=server" stuff anyway. Otherwise, why are you using MVC :)
-
Bender over 15 yearsBecause MVC is a design pattern that has little to do with the whole "purist" write-it-all-in-the-aspx-page argument.
-
Bender over 15 yearsBTW... "Yes, but I said above, it requires <head runat=server> in master page, which is not what I want." You did not say that in your post, you should re-read what you posted and fix your post if that's what you meant to say. Proof-reading FTW.
-
Daniel A. White about 15 yearsDon't do this since JavaScript is never a 100% given.
-
UpTheCreek over 13 yearsYou only need the runat="server" in the head tag if you intend to populate the title from the Title="" directive at the top of your view. Otherwise it's better to leave it out.
-
Rob over 12 yearsThis was perfect - I needed a way to support this in legacy Web Forms-based views. Of course it's already supported via Razor with the ViewBag.propertyName syntax that can lead all Razor views before declaring their layout page. Thanks again!
-
Jony over 9 yearsHi, I like this approach! It works! And I make some modification on it to put the " :: " separator from the master page to the view page(s), thus we can avoid showing an empty " :: " on the home page.