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)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 24449 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

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