I have a custom IHttpHandler that im using to call my controller in MVC3. Problem is that when I call View(“~/path/to/my/view.cshtml”) I get nothing. No error. Nothing, just empty source and a 200 ok.
Since im calling this my self is there some part of the view life-cycle that’s not getting started?
IHttpHandler:
public class MyHttpHandler : IHttpHandler
{
ISimpleController _c;
public SimpleHttpHandler(Controller c)
{
_c = c;
}
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
_c.Get();
}
Code from controller:
public new ActionResult Get()
{
ViewBag.Proof = "Ping";
ViewBag.Of = "Pong";
ViewBag.Life = "Fizz";
return View("~/Views/Shared/WhatAView.cshtml");
}
WhatAView.cshtml:
<ul>
@foreach(var pair in ViewData)
{
<li>@pair.Key : @pair.Value</li>
}
</ul>
The only thing you need is to call ExecuteResult() on your ActionResult. This will force rendering of your view. This is what MVC Framework does after it retrieves ActionResult from a controller.
It’s important that you implemented controller properly w/ valid ControllerContext. I have decompiled ViewResultBase.ExecuteResult() using dotPeek:
You see that this method renders the view into ControllerContext.HttpContext. Ensure that HttpContext is avalaible from within your controller.
As seen above you still can use raw rendering: