We know that Razor view engine converts ViewResult
(IActionResult
) to HTML response which can be sent to users. Below is a picture on Filter pipeline:
My question is, at which stage the Razor view engine will kick in and does the IActionResult
to HTML transformation, right after Endpoint finishes execution or right after Resource filters finishes execution?
Okay, what we definitely know is that ExecuteResult method of the ViewResult class calls into the view engine.
public void ExecuteResult(ControllerContext context)
{
if (string.IsNullOrEmpty(this.ViewName))
ViewName = context.RouteData.GetRequiredString("action");
var viewEngineResult = (ViewEngineResult) null;
if (View == null)
{
viewEngineResult = this.FindView(context);
View = viewEngineResult.View;
}
var output = context.HttpContext.Response.Output;
View.Render(new ViewContext(context, this.View, this.ViewData, this.TempData, output), output);
if (viewEngineResult == null)
return;
viewEngineResult.ViewEngine.ReleaseView(context, this.View);
}
So from the code above I assume that the HTML rendering part happens inside this method. Also we know that IResourceFilter has two methods (the async version IAsyncResultFilter):
void OnResourceExecuting(ResourceExecutingContext context)
void OnResourceExecuted(ResourceExecutedContext context)
The first method OnResourceExecuting executes right before our api endpoint has been called.
[SimpleResourceFilter]
public IActionResult Index()
{
return View();
}
And we can claim that ExecuteResult will be invoked after OnResourceExecuting. To verify what method will be invoked first OnResourceExecuted() or ExecuteResult() you can place debug breakpoints it both of them. The invocation order of this two methods should give you an answer.