ASP.NET: View HTML Page Title

I am trying to find a better way to personalize the <head> element of the page to get rid of extra line breaks caused by <head runat="server"> , so it is formatted correctly.

So far, the only thing I've found works:

  protected override void Render(HtmlTextWriter writer) { StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); base.Render(htmlTextWriter); htmlTextWriter.Close(); string html = stringWriter.ToString(); string newHTML = html.Replace("<title>\r\n\t", "<title>") .Replace("\r\n</title>", "</title>") .Replace("</head>", "\n</head>"); writer.Write(newHTML); } 

Now I have 2 questions:

  • How does the above code affect performance (is it just as viable in a production environment)?
  • Is there a better way to do this, for example, a method that I can override only for custom rendering of <head> ?

Oh yes, ASP.NET MVC is not an option.

EDIT:

I ask about this regarding SEO and just a little bit of perfectionism.

+4
source share
1 answer

From my experience with ASP.NET Forms, implies an irrevocable transfer of authority to HTML output. Better to accept the following:

  • Microsoft controls will sometimes use discounted or incorrect HTML.
  • The Viewstate tag will always be there, even if the ViewState is disabled at the page or site level. Apparently there should be a ViewState to tell ASP.NET that there is no view state.
  • You control the elements of the form not . There will be one form element per page, and it refers to ASP.NET.
  • JavaScript will be used to perform even basic tasks. JavaScript will be unreadable, directly embedded in your page, and at least 50% of it will be extraneous.
  • You will not be able to create fragments of the page, unless you do this through a web service and a lot of kludge. Each .aspx page will have <html> <head> tags and almost always <form> .

If you need precise control over the output of your page, the use of MVC or another structure is mandatory. Even classic ASP will work, but not ASP.NET Forms.

+5
source

Source: https://habr.com/ru/post/1305750/


All Articles