tags:

views:

70

answers:

2

When I create any views in my application in ASP.NET then I see that

<head runat="server">

If I change it to

<head>

what is difference between these two conditions?

+3  A: 

If you define the head tag with the runat attribute set to "server", the value of Title attribute in the page declaration will be shown.

Let's assume you've defined a page with the following declaration on top of the code:

MyPage.aspx:

<%@ Page title="My Website"...

Let's also assume that the master page's head tag is defined as follows:

Site.Master:

<head runat="server"></head>

Then the resulting markup will look like the following:

<head><title>My Website</title></head>

If you define the head tag without the runat attribute set, the title defined by the Title attribute just won't be displayed, and you have to set the title directly in the markup:

<head><title>My Website (Title has been set manually)</title></head>

Update: If you create a new MVC project with Visual Studio, the master page will contain an asp:ContentPlaceHolder within the title tag as default:

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
</head>

The asp:ContentPlaceHolder's content will override the value of the Title attribute you've set in the page declaration, as correctly noted in a comment to this answer by the user Yngvebn

Giu
Yes, this is true. But it seems that if you also define the <title> tag in your view, that overrides the Title="" attribute in the <%@ Page directive.
Yngve B. Nilsen
@Yngvebn The view shouldn't contain the `head` tag, but the master page should, since it's the site-wide page template; the view should serve the content.
Giu
Yeah, I know, but the standard setup in MVC is the asp:ContentPlaceHolder for title, so in the site.master you're getting a <title> <asp:ContentPlaceHolder ID="TitleContent" runat="server" /> </title>This overrides the Title in the page-directive.
Yngve B. Nilsen
@Yngvebn That was exactly what I was going to write, but I somehow managed to lose my reply by doing a deadly " *Previous page in history* " mouse gesture with Opera. Well, back to my reply: Yes, you're right in this case; I actually noticed that the previous edit of my answer didn't include the complete definition of the master page's `head` tag, although I mentioned it in the previous line. Nevertheless, I've updated my answer now; I've included a clean declaration of the `head` tag
Giu
@Giu - Excellent answer :)
Yngve B. Nilsen
@Yngvebn Thanks :) I've updated my answer to add your part about the `ContentPlaceHolder`, including also the source for that part, namely you
Giu
A: 

If you have a tag within the section with a relative url, it fixes up the src path. For example, consider that you may have the head section within a master page which responds to the following URLs.

/foo /foo/bar /foo/bar/baz

But your script src is "../scripts/script.js"

That relative path is only going to be valid for one of those requests. But by using head runat="server", ASP.NET fixes it so it works for all of them.

steven spielberg