Brian Mains
Brian Mains

Reputation: 50728

Razor Web page Rendering

I'm creating a custom web page base class (not to be confused with web forms or MVC), inheriting from System.Web.WebPages.WebPage. What I would like to do is run some code when the web page renders. In web forms, I would override the Render method to do this; however, all I see as an overridable method is RenderPage, which renders a different page within the current rendered page.

Is there a method that gets run during rendering, or is that what the InitializePage method is for?

Thanks.

Upvotes: 1

Views: 427

Answers (1)

Rick Strahl
Rick Strahl

Reputation: 17681

The Razor engine doesn't really 'render' per say. It's basically a code generator that turns your Razor page into executable code of literal strings (HTML) and code blocks (expressions/code). It does this in its Execute() method that gets generated.

So when you create a custom template you might be able to override the Execute() method and do some pre or post processing, but you can't really affect anything inside of the generated code.

Render() in WebForms on the other hand is more of a component based interface since it can potentially intercept rendering from any control each of which has a Render() method that's responsible for generating its own output. In Razor this doesn't make a ton of sense since there are no controls (short of partials).

IAC, I'd recommend taking a look at your Razor pages in teh ASP.NET Temporary Files folder and checking out the source code. That will give you a pretty good idea what the generated code looks like and what you can potentially do in a custom template to intercept the output generation.

Upvotes: 2

Related Questions