public static string Capture(this ActionResult result, ControllerContext controllerContext) { using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) { result.ExecuteResult(controllerContext); return(it.ToString()); } }
/// <summary> /// Captures the contents of an ActionResult as a string /// </summary> /// <param name="result">The ActionResult</param> /// <param name="controllerContext">The current ControllerContext</param> /// <returns>Returns the contents of the ActionResult as a string</returns> public static string Capture(this ActionResult result, ControllerContext controllerContext) { // Use the current response using (var content = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) { // Execute the ActionResult against the current ControllerContext and return the contents result.ExecuteResult(controllerContext); return(content.ToString()); } }
/// <summary> /// Captures the contents of an ActionResult as a string /// </summary> /// <param name="result">The ActionResult</param> /// <param name="controllerContext">The current ControllerContext</param> /// <returns>Returns the contents of the ActionResult as a string</returns> public static string Capture(this ActionResult result, ControllerContext controllerContext) { // Use the current response using (var content = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) { // Execute the ActionResult against the current ControllerContext and return the contents result.ExecuteResult(controllerContext); return content.ToString(); } }
public static string Capture(this ActionResult result, ControllerContext controllerContext) { if (controllerContext == null) { return(""); } using (var capture = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) { result.ExecuteResult(controllerContext); return(capture.ToString()); } }
/// <summary> /// Captures the result and returns a string /// </summary> /// <param name="result">The result to capture</param> /// <param name="controllerContext">The current controller context</param> /// <returns>The result in string form</returns> public static string Capture(this ActionResult result, ControllerContext controllerContext) { if (result is null) { throw new System.ArgumentNullException(nameof(result)); } if (controllerContext is null) { throw new System.ArgumentNullException(nameof(controllerContext)); } using (ResponseCapture it = new ResponseCapture(controllerContext.HttpContext.Response)) { result.ExecuteResult(controllerContext); return(it.ToString()); } }
/// <summary> /// Execute this Action (injecting appropriate parameters are available). /// </summary> /// <param name="controller"> /// The controller that contains this action /// </param> /// <param name="actionDescriptor"> /// The Action in the Controller that is going to be executed /// </param> /// <param name="model"> /// The Model that should be passed to the Action if possible /// </param> /// <param name="message"> /// The Message that should be passed to the Action if possible /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> private string ExecuteAction( ControllerBase controller, ActionDescriptor actionDescriptor, object model = null, object message = null) { // Populate the Action Parameters this.PopulateActionParameters(controller, actionDescriptor, model, message); // Whilst Capturing the output ... using (var it = new ResponseCapture(controller.ControllerContext.RequestContext.HttpContext.Response)) { // Execute the Action (this will automatically invoke any attributes) (controller as IController).Execute(controller.ControllerContext.RequestContext); // Check for valid status codes // EDFUTURE: For now, we only accept methods that produce standard HTML, // 302 Redirects and other possibly valid responses are ignored since we // don't need them at the moment and aren't coding to cope with them if ((HttpStatusCode)controller.ControllerContext.RequestContext.HttpContext.Response.StatusCode == HttpStatusCode.OK) { // And return the generated HTML return(it.ToString()); } return(null); } // FUTURE: Fake HttpContext (continued...) // The code above assumes that a fake HttpContext is in use for this controller // (controller as IController).Execute(controller.ControllerContext.RequestContext); // otherwise we have the problem described in my initial "Fake HttpContext" comments. // // It possible instead to execute the Action directly to avoid the problem // of filter performing unexpected manipulations to the Response and Request, // by using this code: // actionDescriptor.Execute(controller.ControllerContext, parameters); // but of course, this means that our filters (such as our [SetDisplayMode] attribute // are never run and thus, the result is not as expected. // // As an alternative to, it may be possible to recreate the (controller as IController).Execute(...) // ourselves, but bypass the specific filters that cause unexpected manipulations to the Response and Request. // // from Controller.Execute: // ActionInvoker.InvokeAction(ControllerContext, actionName) // from ControllerActionInvoker.InvokeAction: // FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor); // // // Check no authentication issues // AuthenticationContext authenticationContext = InvokeAuthenticationFilters(controllerContext, filterInfo.AuthenticationFilters, actionDescriptor); // if (authenticationContext.Result == null) // { // // // Check no authorization issues // AuthorizationContext authorizationContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor); // if (authorizationContext.Result == null) // { // // // Validate the Request // if (controllerContext.Controller.ValidateRequest) // { // ValidateRequest(controllerContext); // } // // // Run the Action // IDictionary<string, object> parameters = GetParameterValues(controllerContext, actionDescriptor); // ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters); // // // The action succeeded. Let all authentication filters contribute to an action result (to // // combine authentication challenges; some authentication filters need to do negotiation // // even on a successful result). Then, run this action result. // AuthenticationChallengeContext challengeContext = InvokeAuthenticationFiltersChallenge(controllerContext, filterInfo.AuthenticationFilters, actionDescriptor, postActionContext.Result); // InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters, challengeContext.Result ?? postActionContext.Result); // // ... }