Filtering Razor Generated Output
Introduction:
There is no doubt that Razor view engine is
becoming very popular because Razor is speeding things up
just a little bit more. With Razor, sometimes it may be need
to see the Razor generated HTML during debugging.
Sometimes there's a need to filter, change or move the Razor
generated HTML. Sometimes you may need to set the different
properties of your view in a separate file(however I
strongly recommended that you should make your view as
simple as possible). Sometimes you may also need to log the
Razor generated HTML. Sometimes you may need to emit some
HTML in the response output at run time. In this article, I
will show you how to can see and filter the Razor generated
HTML using a simple way.
Description:
Razor views are inherited from WebViewPage class. WebViewPage class provides a ExecutePageHierarchy virtual method which we will override in our custom class. So create a new class file FilterRazorOutput.cs inside Helper folder and add the following code ,
01 |
public
class
FilterRazorOutput : WebViewPage
|
02 |
{
|
03 |
public
override
void
ExecutePageHierarchy()
|
04 |
{
|
05 |
PopContext();
|
06 |
StringWriter writer = new
StringWriter();
|
07 |
PushContext(new
WebPageContext(), writer);
|
08 |
base.ExecutePageHierarchy();
|
09 |
PopContext();
|
10 |
Response.Clear();
|
11 |
string
output = writer.ToString();
|
12 |
output = output.Replace("<body>", "<body><h1>An Example of
Filtering Razor Output</h1>");
|
13 |
Response.Write(output);
|
14 |
Response.End();
|
15 |
}
|
16 |
17 |
public
override
void
Execute()
|
18 |
{
|
19 |
}
|
20 |
}
|
21 |
22 |
public
class
FilterRazorOutput<T> :
WebViewPage<T>
|
23 |
{
|
24 |
public
override
void
ExecutePageHierarchy()
|
25 |
{
|
26 |
PopContext();
|
27 |
StringWriter writer = new
StringWriter();
|
28 |
PushContext(new
WebPageContext(), writer);
|
29 |
base.ExecutePageHierarchy();
|
30 |
PopContext();
|
31 |
Response.Clear();
|
32 |
string
output = writer.ToString();
|
33 |
output = output.Replace("<body>", "<body><h1>An Example of
Filtering Razor Output</h1>");
|
34 |
Response.Write(output);
|
35 |
Response.End();
|
36 |
}
|
37 |
38 |
public
override
void
Execute()
|
39 |
{
|
40 |
}
|
41 |
}
|
The above code simply adds two classes(generic and non generic version of FilterRazorOutput) and then inherits these classes from WebViewPage. These classes simply overrides the ExecutePageHierarchy and Execute methods. Execute is an abstract method. The above classes simply implement this method. During run time, this overriden method will never called because Razor view engine calls the dynamically created Execute method of dynamically generated view class.
ExecutePageHierarchy method is called by Razor view engine during rendering HTML. So it's good place to see and filter the generated HTML. First, ExecutePageHierarchy method(shown above) simply pop the Response.Output text writer from the stack. Then it simply pushes a custom string writer in the stack. Then it call the base implementation of ExecutePageHierarchy method. Then it call the PopContext method again to write the generated HTML in the custom text writer and pop the custom text writer from the stack. Finally it simply filter the generated HTML by appending a h1 tag just after the body tag and then write the filtered HTML to the response and then ends the current request for further execution.
Now just register this new class with your view. So just open your razor view and add this at the top of the page and then run this application and see the effect.
1 |
@inherits
FilteringRazorGeneratedOutput.Helpers.FilterRazorOutput
|
Summary:
In this article I showed you how easily you can
see and filter the HTML generated by Razor view engine. This
will help you when you need to change the generated HTML at
run time or you need to log the generated HTML or when
you need to set the different properties of Razor view at
run time. Hopefully you will enjoy this article too. You can
also download the sample application.