Skip to content

Instantly share code, notes, and snippets.

@MichaelaIvanova
Created March 27, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelaIvanova/8a6e346eac9024a1caf3e860b52ad7a1 to your computer and use it in GitHub Desktop.
Save MichaelaIvanova/8a6e346eac9024a1caf3e860b52ad7a1 to your computer and use it in GitHub Desktop.
Helper for Filter Attribute to change OnResultExecuted
public class MemoryStreamHelper: MemoryStream
{
private readonly Stream _responseStream;
private readonly Dictionary<string, string> _stringReplacementDictionary;
public MemoryStreamHelper(Stream responseStream, Dictionary<string, string> stringReplacementDictionary)
{
_responseStream = responseStream;
_stringReplacementDictionary = stringReplacementDictionary;
}
public override void Write(byte[] buffer, int offset, int count)
{
string html = Encoding.UTF8.GetString(buffer);
//replace values in html
html = _stringReplacementDictionary.Aggregate(html, (current, item) => Regex.Replace(current, item.Key + "(?!\" class=\"language)", item.Value));
buffer = Encoding.UTF8.GetBytes(html);
_responseStream.Write(buffer, offset, buffer.Length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment