Insert html title and meta description server side in ASP.NET?

时间:2017-08-30 20:40:31

标签: c# asp.net iis

I am using IIS 7 and ASP.NET.

What I would like to do is being able to server side change the title and the meta description of my index.html file, before it is shown in a webbrowser.

string cleanTitle = Regex.Replace(BookRec.TitleName, "[^\\w\\. _]", "");

I understand I can create either a http handler, http module or http filter in ASP.NET to do this hook up on the request. I found examples of appending html this way, but none showing how to replace/insert text for existing html.

I imagine pseudo code could look like this:

( ) _ , - .

How can I do this?

SOLUTION

I created a content parser module based on the code and example from this article. Unfortunately the author left out the part about adding the module to web.config, so you should remember to implement that part yourself.

What I did was to put the title and description change into the Write method based on the url segment and a database lookup.

1 个答案:

答案 0 :(得分:0)

Server side, you can set the Page.Title in the Master page's Page_Load event to change the title.

Also server side, use the HtmlMeta class to change meta properties as described here: https://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlmeta(v=vs.110).aspx

In Angular you can use the setTitle method of the Title service in Browser platform: https://angular.io/guide/set-document-title

Also Angular Browser platform, Use the Meta service addTag method to manipulate meta tags in Angular: https://angular.io/api/platform-browser/Meta