[英]How can I intercept the HTML from a ViewResult, modify it and serve it up?
I'm writing a simple CMS. 我正在写一个简单的CMS。
I want to be able to load a View, having it included inside a master page, and then scan the HTML so that I can replace some custom tags (like {{blog}} with my own blog output) and then serve it up to the browser. 我希望能够加载一个View,将其包含在母版页中,然后扫描HTML以便我可以用我自己的博客输出替换一些自定义标签(如{{blog}}),然后将其提供给浏览器。
How can I get access to the HTML from the ViewResult in order to intercept it? 如何从ViewResult访问HTML以拦截它?
That's going to be tricky because the ViewResult writes its response directly to the Response.Stream. 这将是棘手的,因为ViewResult将其响应直接写入Response.Stream。 So you'll probably have to deal with the Response.Filter property to output the ViewResult to a MemoryStream so you can manipulate the content before returning a ContentResult.
因此,您可能必须处理Response.Filter属性以将ViewResult输出到MemoryStream,以便您可以在返回ContentResult之前操作内容。 All this would happen in OnResultExecuting probably.
所有这些都可能发生在OnResultExecuting上。
Sounds like you want to write an ActionFilterAttribute
. 听起来你想写一个
ActionFilterAttribute
。 This attribute has the following methods: 该属性具有以下方法:
ActionResult
is rendered. ActionResult
之前调用。 There is an example here which returns either JSON or XML data depending on the "Content-type" header: Create REST API using ASP.NET MVC that speaks both Json and plain Xml 这里有一个示例,它返回JSON或XML数据,具体取决于“Content-type”标题: 使用ASP.NET MVC创建REST API,它同时使用Json和plain Xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.