Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, Darin's answer got me started, but there's a lot more detail to the final solution, so I'm adding a separate answer. This one is complex, so bear with me.</p> <p>There are 4 steps to getting the ViewResult from a URL:</p> <ol> <li><p>Mock the RequestContext via the routing system (Darin's answer got me started on this).</p> <pre><code> Uri uri = new Uri(MyStringUrl); var request = new HttpRequest(null, uri.Scheme + "://" + uri.Authority + uri.AbsolutePath, string.IsNullOrWhiteSpace(uri.Query) ? null : uri.Query.Substring(1)); var response = new HttpResponse(new StringWriter()); var context = new HttpContext(request, response); var contextBase = new HttpContextWrapper(context); var routeData = System.Web.Routing.RouteTable.Routes.GetRouteData(contextBase); // We shouldn't have to do this, but the way we are mocking the request doesn't seem to pass the querystring data through to the route data. foreach (string key in request.QueryString.Keys) { if (!routeData.Values.ContainsKey(key)) { routeData.Values.Add(key, request.QueryString[key]); } } var requestContext = new System.Web.Routing.RequestContext(contextBase, routeData); </code></pre></li> <li><p>Subclass your controller. Add a public method that allows you to call the protected Execute(RequestContext) method.</p> <pre><code>public void MyExecute(System.Web.Routing.RequestContext requestContext) { this.Execute(requestContext); } </code></pre></li> <li><p>In the same subclassed controller, Add a public event that hooks in to the protected OnActionExecuted event. This allows you to reach in a grab the ViewResult via the ActionExecutedContext.</p> <pre><code>public delegate void MyActionExecutedHandler(ActionExecutedContext filterContext); public event MyActionExecutedHandler MyActionExecuted; protected override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); if (MyActionExecuted != null) { MyActionExecuted(filterContext); } } </code></pre></li> <li><p>Tie everything together by instantiating an instance of the new controller subclass, adding an event handler, and calling the new public execute method (passing in the mocked RequestContext). The event handler will give you access to the ViewResult. </p> <pre><code> using (MyCompany.Controllers.MyController c = new Controllers.MyController()) { c.MyActionExecuted += GrabActionResult; try { c.MyExecute(requestContext); } catch (Exception) { // Handle an exception. } } </code></pre></li> </ol> <p>and here's the event handler:</p> <pre><code> private void GrabActionResult(System.Web.Mvc.ActionExecutedContext context) { if (context.Result.GetType() == typeof(ViewResult)) { ViewResult result = context.Result as ViewResult; } else if (context.Result.GetType() == typeof(RedirectToRouteResult)) { // Handle. } else if (context.Result.GetType() == typeof(HttpNotFoundResult)) { // Handle. } else { // Handle. } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload