第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

一個頁面標(biāo)題和過濾輸出的解決方案

標(biāo)簽:
架構(gòu)

上一篇说到:为了可扩展与方便大伙,我定义了一个抽象类,先实现了三个正则用于截取标题,说明,和关键字。

抽象类代码简洁如下:

复制代码

public abstract class ReplaceTextListBase
    {
        /// <summary>
        /// 将被返回的替换文本集合列表
        /// </summary>
        public Dictionary<string, string> replaceTextList = new Dictionary<string, string>();
        /// <summary>
        /// 获取当前请求页面的url信息
        /// </summary>
        public Uri PageUrl { get { return HttpContext.Current.Request.Url; } }
        /// <summary>
        /// 获取html的title的正则
        /// </summary>
        public string TitleRegex { get { return "<title.*>.*</title>"; } }
        public string TitleFormat(string titleText)
        {
            return "<title>" + titleText + "</title>";
        }
        /// <summary>
        /// 获取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>
        /// 获取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>
        /// 复写此方法,调用replaceTextList.add()方法后,return replaceTextList;
        /// </summary>
        /// <returns></returns>
        public virtual Dictionary<string, string> GetReplaceTextList()
        {
            return replaceTextList;
        }
    }

复制代码

 

 

抽象类后,留下一个虚方法GetReplaceTextList(), 这是重点

现在看一下我的示例中的子类的实现,继承自抽象类,复写虚方法:

复制代码

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;
        }
}复制代码

 

代码解析:

例子中的子类实现很简单,就复写了一个虚方法,最终页面的输出标题为:TitleRegex。其它两个同理。
如果要替换其它或过滤文件,只要写多几个add方法把要替换的文字给替换掉就行了,具体也可以结合下数据库或其它文件操作

 

另外说明:

例子上,直接就定死了标题输出为:TitleRegex,这里可以结合自己的需要,替换成任意字符串。

提示:抽象类里还留下了PageUr吧,可以根据Url查出Title和description和keyword来实现自己的扩展。

 

 

另外给出一些我早期实现的思路:

建数据库表,对url主机头进行分类管理,自己定义替换字符等,最后查询与替换。

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消