My textbook shows an example of how to write custom view engine(implements IViewEngine
, and the view it uses is a class that implements IView
public interface IViewEngine{
ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage);
ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage);
}
and
public interface IView {
string Path { get; }
Task RenderAsync(ViewContext context);
}
and below is a custom view that implements IView
public class DebugDataView : IView
{
public string Path => String.Empty;
public async Task RenderAsync(ViewContext context)
{
context.HttpContext.Response.ContentType = "text/plain";
StringBuilder sb = new StringBuilder();
sb.AppendLine("---Routing Data---");
foreach (var kvp in context.RouteData.Values)
{
sb.AppendLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
sb.AppendLine("---View Data---");
foreach (var kvp in context.ViewData)
{
sb.AppendLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
sb.AppendLine(context.ViewData.Model.ToString());
await context.Writer.WriteAsync(sb.ToString());
}
}
I have some questions
Q1-does mean that after the content in .cshtml view files is parsed by razor, then razor will create a View Object that implements IView behind the scene so that real content is implemented in RenderAsync?
Q2-the FindView
or GetView
method returns a ViewEngineResult
object, then who will be turning ViewEngineResult
object into response(html for clients), MVC or Razor Engine?