Esempio n. 1
0
        private async Task RenderView(RazorFormat format, IRequest req, IResponse res, IView view)
        {
            res.ContentType = MimeTypes.Html;
            var model = Model;

            if (model == null)
            {
                req.Items.TryGetValue("Model", out model);
            }

            ViewDataDictionary viewData = null;

            if (model == null)
            {
                var razorView  = view as RazorView;
                var genericDef = razorView.RazorPage.GetType().FirstGenericType();
                var modelType  = genericDef?.GetGenericArguments()[0];
                if (modelType != null && modelType != typeof(object))
                {
                    model = await DeserializeHttpRequestAsync(modelType, req, req.ContentType);

                    viewData = RazorFormat.CreateViewData(model);
                }
            }

            if (viewData == null)
            {
                viewData = new ViewDataDictionary <object>(
                    metadataProvider: new EmptyModelMetadataProvider(),
                    modelState: new ModelStateDictionary());

                foreach (var cookie in req.Cookies)
                {
                    viewData[cookie.Key] = cookie.Value.Value;
                }
                foreach (var header in req.Headers.AllKeys)
                {
                    viewData[header] = req.Headers[header];
                }
                foreach (var key in req.QueryString.AllKeys)
                {
                    viewData[key] = req.QueryString[key];
                }
                foreach (var key in req.FormData.AllKeys)
                {
                    viewData[key] = req.FormData[key];
                }
                foreach (var entry in req.Items)
                {
                    viewData[entry.Key] = entry.Value;
                }
            }

            await format.RenderView(req, res.OutputStream, viewData, view);
        }
Esempio n. 2
0
        public override void ProcessRequest(IRequest req, IResponse res, string operationName)
        {
            var format = HostContext.GetPlugin <RazorFormat>();

            try
            {
                if (viewEngineResult == null)
                {
                    if (PathInfo == null)
                    {
                        throw new ArgumentNullException(nameof(PathInfo));
                    }

                    viewEngineResult = format.GetPageFromPathInfo(PathInfo);

                    if (viewEngineResult == null)
                    {
                        throw new ArgumentException("Could not find Razor Page at " + PathInfo);
                    }
                }

                res.ContentType = MimeTypes.Html;
                var model = Model;
                if (model == null)
                {
                    req.Items.TryGetValue("Model", out model);
                }

                ViewDataDictionary viewData = null;
                if (model == null)
                {
                    var razorView  = viewEngineResult.View as RazorView;
                    var genericDef = razorView.RazorPage.GetType().FirstGenericType();
                    var modelType  = genericDef?.GetGenericArguments()[0];
                    if (modelType != null && modelType != typeof(object))
                    {
                        model    = DeserializeHttpRequest(modelType, req, req.ContentType);
                        viewData = RazorFormat.CreateViewData(model);
                    }
                }

                if (viewData == null)
                {
                    viewData = new ViewDataDictionary <object>(
                        metadataProvider: new EmptyModelMetadataProvider(),
                        modelState: new ModelStateDictionary());

                    foreach (string header in req.Headers)
                    {
                        viewData[header] = req.Headers[header];
                    }
                    foreach (string key in req.QueryString)
                    {
                        viewData[key] = req.QueryString[key];
                    }
                    foreach (string key in req.FormData)
                    {
                        viewData[key] = req.QueryString[key];
                    }
                }

                format.RenderView(req, viewData, viewEngineResult.View);
            }
            catch (Exception ex)
            {
                //Can't set HTTP Headers which are already written at this point
                req.Response.WriteErrorBody(ex);
            }
        }