As mentioned in the previous article: For scalability and convenience, I have defined an abstract class and implemented three regular expressions to intercept titles, descriptions, and keywords.
The abstract class code is concise as follows:
Public abstract class ReplaceTextListBase
{
/// <Summary>
/// List of replaced text sets to be returned
/// </Summary>
Public Dictionary <string, string> replaceTextList = new Dictionary <string, string> ();
/// <Summary>
/// Obtain the url Information of the current request page
/// </Summary>
Public Uri PageUrl {get {return HttpContext. Current. Request. Url ;}}
/// <Summary>
/// Obtain the Regular Expression of html title
/// </Summary>
Public string TitleRegex {get {return "<title. *>. * </title> ";}}
Public string TitleFormat (string titleText)
{
Return "<title>" + titleText + "</title> ";
}
/// <Summary>
/// Obtain the Regular Expression of html Description
/// </Summary>
Public string DescriptionRegex {get {return "<meta [^ <>] + name = [\" \ '] description [^ <>] * [/]> ";}}
Public string DescriptionFormat (string descriptionText)
{
Return "<meta id = \" description \ "name = \" description \ "content = \" "+ descriptionText +" \ "/> ";
}
/// <Summary>
/// Obtain the regular expression of the html Keyword
/// </Summary>
Public string KeywordRegex {get {return "<meta [^ <>] + name = [\" \ '] keywords [^ <>] * [/]> ";}}
Public string KeywordFormat (string keywordText)
{
Return "<meta id = \" keywords \ "name = \" keywords \ "content = \" "+ keywordText +" \ "/> ";
}
/// <Summary>
/// Re-write this method. After the replaceTextList. add () method is called, return replaceTextList;
/// </Summary>
/// <Returns> </returns>
Public virtual Dictionary <string, string> GetReplaceTextList ()
{
Return replaceTextList;
}
}
After the abstract class, leave a virtual method GetReplaceTextList (), which is the focus
Now let's take a look at the implementation of the sub-classes in my example, inherit from the abstract class, and rewrite the virtual method:
Public class ReplaceTextList: ReplaceTextListBase
{
Public override System. Collections. Generic. Dictionary <string, string> GetReplaceTextList ()
{
ReplaceTextList. Add (TitleRegex, TitleFormat ("TitleRegex "));
ReplaceTextList. Add (DescriptionRegex, DescriptionFormat ("descriptionttest "));
ReplaceTextList. Add (KeywordRegex, KeywordFormat ("keywordadfdfdf "));
Return replaceTextList;
}
}
Code parsing:
The subclass implementation in the example is very simple, and a virtual method is rewritten. The final output title of the page is TitleRegex. The other two are the same.
If you want to replace other or filter files, you only need to write several add methods to replace the text to be replaced. You can also perform operations in combination with the database or other files.
Note:
In this example, the title output is TitleRegex, which can be replaced with any string based on your needs.
Tip: PageUr is left in the abstract class. You can find the Title, description, and keyword Based on the Url to implement your own extension.
In addition, I provided some ideas for early implementation:
Create a database table, classify and manage the url Host Header, define replacement characters, and finally query and replace the host.