public async Task<HttpResponseMessage> RenderResponse(HttpRequestMessage request, string pageName, bool isPost, CancellationToken cancellationToken, object model = null, Type modelType = null, AdoDataConnection database = null)
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
string content, fileExtension = FilePath.GetExtension(pageName).ToLowerInvariant();
bool embeddedResource = pageName.StartsWith("@");
Tuple<Type, Type> pagedViewModelTypes;
if (embeddedResource)
pageName = pageName.Substring(1).Replace('/', '.');
response.RequestMessage = request;
switch (fileExtension)
{
case ".cshtml":
m_pagedViewModelTypes.TryGetValue(pageName, out pagedViewModelTypes);
content = await new RazorView(embeddedResource ? RazorEngine<CSharpEmbeddedResource>.Default : m_razorEngineCS, pageName, model, modelType, pagedViewModelTypes?.Item1, pagedViewModelTypes?.Item2, database, OnExecutionException).ExecuteAsync(request, isPost, cancellationToken);
response.Content = new StringContent(content, Encoding.UTF8, "text/html");
break;
case ".vbhtml":
m_pagedViewModelTypes.TryGetValue(pageName, out pagedViewModelTypes);
content = await new RazorView(embeddedResource ? RazorEngine<VisualBasicEmbeddedResource>.Default : m_razorEngineVB, pageName, model, modelType, pagedViewModelTypes?.Item1, pagedViewModelTypes?.Item2, database, OnExecutionException).ExecuteAsync(request, isPost, cancellationToken);
response.Content = new StringContent(content, Encoding.UTF8, "text/html");
break;
case ".ashx":
await ProcessHTTPHandlerAsync(pageName, embeddedResource, request, response, cancellationToken);
break;
default:
string fileName = GetResourceFileName(pageName, embeddedResource);
if (ClientCacheEnabled)
{
long responseHash;
if (!m_etagCache.TryGetValue(fileName, out responseHash))
{
if (!ResourceExists(fileName, embeddedResource))
{
response.StatusCode = HttpStatusCode.NotFound;
break;
}
await Task.Run(async () =>
{
using (Stream source = await OpenResourceAsync(fileName, embeddedResource, cancellationToken))
{
// Calculate check-sum for file
const int BufferSize = 32768;
byte[] buffer = new byte[BufferSize];
Crc32 calculatedHash = new Crc32();
int bytesRead = await source.ReadAsync(buffer, 0, BufferSize, cancellationToken);
while (bytesRead > 0)
{
calculatedHash.Update(buffer, 0, bytesRead);
bytesRead = await source.ReadAsync(buffer, 0, BufferSize, cancellationToken);
}
responseHash = calculatedHash.Value;
m_etagCache.TryAdd(fileName, responseHash);
OnStatusMessage($"Cache [{responseHash}] added for file \"{fileName}\"");
}
}, cancellationToken);
}
if (PublishResponseContent(request, response, responseHash))
{
response.Content = new StreamContent(await OpenResourceAsync(fileName, embeddedResource, cancellationToken));
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(pageName));
}
}
else
{
if (!ResourceExists(fileName, embeddedResource))
{
response.StatusCode = HttpStatusCode.NotFound;
break;
}
response.Content = new StreamContent(await OpenResourceAsync(fileName, embeddedResource, cancellationToken));
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(pageName));
}
break;
}
return response;
}