mvc可以提价html标签,MVC输出前对html修改例子

【实例简介】原理:ActionFilterAttribute

【实例截图】

【核心代码】

public class ReplaceTagsFilter : MemoryStream

{

private readonly Stream _response;

public ReplaceTagsFilter(Stream response)

{

_response = response;

}

public override void Write(byte[] buffer, int offset, int count)

{

var html = Encoding.UTF8.GetString(buffer);

html = ReplaceTags(html);

buffer = Encoding.UTF8.GetBytes(html);

_response.Write(buffer, offset, buffer.Length);

}

private string ReplaceTags(string html)

{

// TODO: go ahead and implement the filtering logic

throw new NotImplementedException();

}

}

public class ReplaceTagsAttribute : ActionFilterAttribute

{

public override void OnActionExecuting(ActionExecutingContext filterContext)

{

var response = filterContext.HttpContext.Response;

response.Filter = new ReplaceTagsFilter(response.Filter);

}

}

[ReplaceTags]

public ActionResult Index()

{

return View();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值