예제 #1
0
        /// <summary>
        /// Used when changes are required to render a view as a string, without altering the ControllerContext for the actual request.
        /// </summary>
        private ExtendedControllerContext CloneControllerContext()
        {
            ControllerContext copy = new ControllerContext(Request.RequestContext, this);
            // ControllerContext property might be null, that's why we don't use that overload.

            /*
             * We would never want to have partial views rendered outside of the view, i.e. with @Html.Partial(),
             * emitting JavaScript code into the actual view, because that's not what would be intended.
             *
             * This simple overwrite fixes a few scenarios collaterally:
             *	- JavaScript partials don't try to emit JavaScript, because they are rendered using the ExtendedControllerContext.
             *	- The same applies to both partial and JavaScript views being rendered by the AjaxTransformAttribute engine.
             *
             * In addition, the only scenario likely to ever want this to happen would be something like an AJAX call,
             * where we'd like to return both the HTML and the JavaScript, but that can already be handled through the existing methods.
             */
            ExtendedControllerContext context = new ExtendedControllerContext(copy);

            // required for requests to *.cshtml physical files, in order to render the error view.
            if (!context.RouteData.Values.ContainsKey(Resources.Constants.RouteDataController))
            {
                context.RouteData.Values.Add(Resources.Constants.RouteDataController, Resources.Constants.RouteDataControllerNotFound);
                context.RouteData.Values.Add(Resources.Constants.RouteDataAction, Resources.Constants.RouteDataActionNotFound);
            }
            return(context);
        }
예제 #2
0
        /// <summary>
        /// Using a single controller context, renders both HTML and JavaScript for a PartialView, separating its concerns in order to deliver a concise result.
        /// </summary>
        internal SeparationOfConcernsResult ViewSeparationOfConcerns(string viewName, string controller, object model)
        {
            ExtendedControllerContext context  = CloneControllerContext(); // use the same context in order to fetch JavaScript code for the PartialView being rendered.
            IJavaScriptHelper         jsHelper = Common.IoC.IoC.Container.Resolve <IJavaScriptHelper>();
            string html = ViewString(viewName, controller, model, context);
            string js   = jsHelper.Emit(context.Guid).ToHtmlString();

            return(new SeparationOfConcernsResult
            {
                Html = html,
                JavaScript = js
            });
        }
예제 #3
0
        /// <summary>
        /// Renders a view as a string.
        /// </summary>
        /// <param name="viewName">The view name. </param>
        /// <param name="controller">The controller name.</param>
        /// <param name="model">The view model.</param>
        /// <param name="context">The controller context.</param>
        public string ViewString(string viewName, string controller, object model, ExtendedControllerContext context)
        {
            Func <string, ControllerContext, ViewEngineResult> findView = (name, ctx) => ViewEngines.Engines.FindView(ctx, name, null);

            return(InternalGetViewString(viewName, controller, model, findView, context));
        }
예제 #4
0
        /// <summary>
        /// Renders a view as a string.
        /// </summary>
        /// <param name="viewName">The view name. </param>
        /// <param name="controller">The controller name.</param>
        /// <param name="model">The view model.</param>
        public string ViewString(string viewName, string controller, object model)
        {
            ExtendedControllerContext context = CloneControllerContext();

            return(ViewString(viewName, controller, model, context));
        }
예제 #5
0
        /// <summary>
        /// Gets a view string aftering producing a ControllerContext where it should be rendered.
        /// </summary>
        private string InternalGetViewString(string viewName, string controller, object model, Func <string, ControllerContext, ViewEngineResult> findView, ExtendedControllerContext context)
        {
            if (!controller.NullOrEmpty())
            {
                context.RouteData.Values["controller"] = controller;
            }
            if (viewName.NullOrEmpty())
            {
                viewName = context.RouteData.GetActionString();
            }
            IView  view   = findView(viewName, context).View;
            string result = RenderViewToString(context, view, model);

            return(result);
        }
예제 #6
0
        /// <summary>
        /// Renders a partial view as a string.
        /// </summary>
        /// <param name="partialViewName">Either the fully qualified virtual path to the View, or the View name in the current context.</param>
        /// <param name="controller">The controller name.</param>
        /// <param name="model">The view model.</param>
        public string PartialViewString(string partialViewName, string controller, object model)
        {
            ExtendedControllerContext context = CloneControllerContext();

            return(PartialViewString(partialViewName, controller, model, context));
        }