[Mono-aspnet-list] How to fix exception in System.Web or other way to render view in WebAPI

Andrus kobruleht2 at hot.ee
Tue Nov 11 07:07:56 UTC 2014


Code from
is used to render Razor view to string in ASP.NET MVC4 WebAPI controller. It
creates fake controller since WebAPI
does not have regular controller reuired by Razor:

     public class ValuesController : ApiController

            public HttpResponseMessage Get()
                var body = RenderViewToString("Values",
"~/Views/Home/Index.cshtml", new object());
                return Request.CreateResponse(HttpStatusCode.OK, new {
content = body });

            public static string RenderViewToString(string controllerName,
string viewName, object viewData)
                using (var writer = new StringWriter())
                    var routeData = new RouteData();
                    routeData.Values.Add("controller", controllerName);
                    var fakeControllerContext = new ControllerContext(new
HttpContextWrapper(new HttpContext(new HttpRequest(null,
"http://google.com", null), new HttpResponse(null))), routeData, new
                    var razorViewEngine = new RazorViewEngine();
                    var razorViewResult =
razorViewEngine.FindView(fakeControllerContext, viewName, "", false);

                    var viewContext = new ViewContext(fakeControllerContext,
razorViewResult.View, new ViewDataDictionary(viewData), new
TempDataDictionary(), writer);
                    razorViewResult.View.Render(viewContext, writer);
                    return writer.ToString();

        public class FakeController : ControllerBase  { protected override
void ExecuteCore() { }  }

In ASP.NET it works OK .
In Mono it causes NullreferenceException at line
`razorViewResult.View.Render(viewContext, writer);`

    at System.Web.HttpRequest.get_IsLocal () <0x00026>
    at System.Web.HttpRequestWrapper.get_IsLocal () <0x0001b>
    at System.Web.WebPages.WebPageHttpHandler.ShouldGenerateSourceHeader
(System.Web.HttpContextBase) <0x0001f>
    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy () <0x0002b>
    at System.Web.Mvc.WebViewPage.ExecutePageHierarchy () <0x0005f>
    at System.Web.WebPages.StartPage.RunPage () <0x0001f>
    at System.Web.WebPages.StartPage.ExecutePageHierarchy () <0x00057>
    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy
    at System.Web.Mvc.RazorView.RenderView
(System.Web.Mvc.ViewContext,System.IO.TextWriter,object) <0x00227>
    at System.Web.Mvc.BuildManagerCompiledView.Render
(System.Web.Mvc.ViewContext,System.IO.TextWriter) <0x000a8>
    at Controllers.APIBase.RenderViewToString (string,string,object)

Mono 3.2.5 in Debian amd64 is used.
It looks like exception occurs in IsLocal() getter


How to fix or diagnose this ?

If similar method is called from regular MVC4 controller using regular
controller context, it works in Mono also.
How to make it work from WebAPI controller also ?
Or is it possible to use workaround, e.q call MVC4 controller to render and
grab its result or use RazorEngine .


More information about the Mono-aspnet-list mailing list