A solution for page title and filtering output (below)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.