Bug 24449 - NullReferenceException rendering view from Web API
Summary: NullReferenceException rendering view from Web API
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Web (show other bugs)
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-11-11 16:58 UTC by eetasoft
Modified: 2014-11-11 16:58 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description eetasoft 2014-11-11 16:58:13 UTC
When rendering Razor view in MVC4 using WebAPI, NullReference Exception occurs is System.Web.dll

Step to reproduce:

1. In MVC4 application, render Razor view to string using code:

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 FakeController());
                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() { }  }


Observed:

 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 (System.Web.WebPages.WebPageContext,System.IO.TextWriter,System.Web.WebPages.WebPageRenderingBase) <0x000e5>
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) 

It looks like exception occurs in IsLocal() getter

https://github.com/mono/mono/blob/mono-3.2.5-branch/mcs/class/System.Web/System.Web/HttpRequest.cs#L1671


This is reported also in

http://stackoverflow.com/questions/26854342/exception-in-mono-system-web-rendering-view-in-asp-net-web-api-controller
http://forums.asp.net/t/2017674.aspx?How+to+return+rendered+razor+view+from+Web+API+controller

Note You need to log in before you can comment on or make changes to this bug.