37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
|
using System;
|
|||
|
using ln.http;
|
|||
|
using ln.http.exceptions;
|
|||
|
using ln.http.router;
|
|||
|
using ln.templates.html;
|
|||
|
|
|||
|
namespace ln.templates.http
|
|||
|
{
|
|||
|
public delegate void PrepareRenderContext(TemplateRouter templateRouter,TemplateDocument templateDocument, RenderContext renderContext);
|
|||
|
public class TemplateRouter : IHttpRouter
|
|||
|
{
|
|||
|
ITemplateSource TemplateSource { get; }
|
|||
|
|
|||
|
public event PrepareRenderContext OnPrepareRenderContext;
|
|||
|
|
|||
|
public TemplateRouter(ITemplateSource templateSource)
|
|||
|
{
|
|||
|
TemplateSource = templateSource;
|
|||
|
}
|
|||
|
|
|||
|
public HttpResponse Route(HttpRoutingContext routingContext, HttpRequest httpRequest)
|
|||
|
{
|
|||
|
TemplateDocument document = TemplateSource.GetTemplateByPath(routingContext.Path);
|
|||
|
if (document != null)
|
|||
|
{
|
|||
|
HttpResponse response = new HttpResponse(httpRequest);
|
|||
|
RenderContext renderContext = new RenderContext(response.ContentWriter, TemplateSource);
|
|||
|
OnPrepareRenderContext?.Invoke(this, document, renderContext);
|
|||
|
document.RenderTemplate(renderContext);
|
|||
|
|
|||
|
return response;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|