I'm porting an ASP.NET HTTP handler to an ASP.NET Core middleware class. The majority of the time, the handler responds with a simple UTF-8 text response. Sometimes, the HTTP handler would stuff a big object graph in the ASP.NET Session
state, then do a Response.Redirect
to an ASPX Web Form page. The web form page extracts the object graph from the Session
state and renders it.
It seems to me that the redirection is unnecessary, plus it forces me to include session state in my application, which I don't want. What I do want is to translate the Web Forms page to a Razor page, and then somehow invoke the Razor page directly to render my object graph. Can my middleware in essence yield control to Razor, specifying: "here is the cshtml page I want you to render and here is a object graph?"
I can easily create PageModel
class with a property ObjectGraph
, but how do I yield over control to Razor to render that cshtml page?
public async Task Invoke(HttpContext ctx) {
var objectGraph = ComputeObjectGraph(ctx.Request);
if (IsSimpleRendering(ctx.Request)) {
async DoSimpleRendering(objectGraph, ctx.Response);
} else {
var razorPageModel = new HtmlRendererModel(objectGraph);
// TODO: hand off to Razor, using "htmlrenderer.cshtml" as a template.
// async DoHtmlRendering("htmlrenderer.cshtml", razorPageModel, ctx);
}
}