I'm writing an html helper extension for MVC5. I need to access the ViewContext's stream to edit it. (like a textreader)
htmlHelper.ViewContext.Writer.Write("<div>");
Is there any way to access the stream; so that I can manipulate or add some html code above the last "div" tag in the example ?
I was not able to find any clue about at where I can find the stream that textwriter writes to?
It depends on what view engine you are using but if you write an html helper, it will be a lot easier. See Creating Custom HTML Helpers
Simple html helpers can be written like this:
In Razor, You can also just call
@Write()and it will encode the string to HTML. If you need a TextWriter, referenceContext.Response.Outputbut this will not allow you to manipulate the HTML inline like an HtmlHelper does - it's intended more for overriding the normal handler.or just the
Outputproperty ofWebPageBasewhich is also a TextWriter