GSF.Web.Hosting.WebServer.RenderResponse C# (CSharp) Method

RenderResponse() public method

Renders an HTTP response for a given request.
public RenderResponse ( HttpRequestMessage request, string pageName, bool isPost, CancellationToken cancellationToken, object model = null, Type modelType = null, AdoDataConnection database = null ) : Task
request System.Net.Http.HttpRequestMessage HTTP request message.
pageName string Name of page to render.
isPost bool trueif is HTTP post; otherwise, false.
cancellationToken System.Threading.CancellationToken Propagates notification from client that operations should be canceled.
model object Reference to model to use when rendering Razor templates, if any.
modelType System.Type Type of , if any.
database AdoDataConnection to use, if any.
return Task
        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;
        }

Usage Example

Example #1
0
 public Task <HttpResponseMessage> GetPage(string pageName, CancellationToken cancellationToken)
 {
     return(m_webServer.RenderResponse(Request, pageName, false, cancellationToken, Model, ModelType, Database));
 }
All Usage Examples Of GSF.Web.Hosting.WebServer::RenderResponse