Beispiel #1
0
        public StringResult Page(XrcUrl url, object parameters = null, IContext callerContext = null)
        {
            try
            {
                var parentRequest = callerContext == null ? null : callerContext.Request;
                var parentResponse = callerContext == null ? null : callerContext.Response;

                using (var stream = new MemoryStream())
                {
                    var request = new XrcRequest(url, parentRequest: parentRequest);
                    var response = new XrcResponse(stream, parentResponse: parentResponse);
                    var context = new Context(request, response);

                    context.CallerContext = callerContext;
                    AddParameters(context, parameters);

                    ProcessRequest(context);

                    context.CheckResponse();

                    response.Flush();

                    stream.Seek(0, SeekOrigin.Begin);

                    using (StreamReader reader = new StreamReader(stream, response.ContentEncoding))
                    {
                        return new StringResult(reader.ReadToEnd(), response.ContentEncoding, response.ContentType);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new PageException(url.AppRelaviteUrl, ex);
            }
        }
Beispiel #2
0
 public Context(XrcRequest request, XrcResponse response)
 {
     _request = request;
     _response = response;
 }
Beispiel #3
0
        private void RenderLayout(IContext childContext, PageAction childAction, Dictionary<string, object> childModules)
        {
            XrcResponse currentResponse = childContext.Response;

            // If a parent is defined call first it using the current response
            // and defining a SlotCallback event that output the current slot inline.
            // The event will be called from the layout action by using Cms.Slot().
            // Parameters will be also copied from slot to layout.

            XrcUrl layoutPage = childContext.Page.GetPageUrl(childAction.Layout.ToLower());
            Context layoutContext = new Context(new XrcRequest(layoutPage, parentRequest: childContext.Request), currentResponse);
            layoutContext.CallerContext = childContext;
            foreach (var item in childContext.Parameters)
                layoutContext.Parameters.Add(new ContextParameter(item.Name, item.Type, item.Value));

            layoutContext.SlotCallback = (s, e) =>
            {
                var childResult = new StringResult();
                using (MemoryStream stream = new MemoryStream())
                {
                    XrcResponse response = new XrcResponse(stream, parentResponse: currentResponse);
                    childContext.Response = response;

                    ViewDefinition viewDefinition = childAction.Views[e.Name];
                    if (viewDefinition != null)
                        ExecuteView(childContext, childAction, viewDefinition, childModules);
                    else
                        throw new ApplicationException(string.Format("Slot '{0}' not found.", e.Name));

                    childResult.ContentEncoding = response.ContentEncoding;
                    childResult.ContentType = response.ContentType;

                    response.Flush();

                    stream.Seek(0, SeekOrigin.Begin);

                    using (StreamReader reader = new StreamReader(stream, response.ContentEncoding))
                    {
                        childResult.Content = reader.ReadToEnd();
                    }
                }

                e.Result = childResult;
            };

            try
            {
                ProcessRequest(layoutContext);
                layoutContext.CheckResponse();
            }
            catch (Exception ex)
            {
                // Set a generic status code. We don't want to expose directly parent StatusCode like redirect
                //  otherwise the client is redirected to a wrong page (the parent page).
                currentResponse.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                throw new PageException(layoutPage.ToString(), ex);
            }
        }
Beispiel #4
0
 public Context(HttpContextBase httpContext)
 {
     _request = new XrcRequest(httpContext.Request);
     _response = new XrcResponse(httpContext.Response);
 }