Another ASP.Net MVC bug: Rendering views to different output source

time to read 1 min | 168 words

Take a look at the following code. What would you expect the result of this code to be?

image

Leaving aside the question of exactly what I am doing here, or why. What I thought this should do was to render the partial view into the string writer.

The method signature of Render most strongly suggest that this is what it would do. What it actually does is to render the partial view directly into the response. Following the code a bit more, it looks like you literally cannot do this.

ASP.Net MVC views are hard coded to use the current request ( ViewPage.RenderView ). I checked a bit, and without basically faking the entire HttpContext and all its associated objects, that doesn't seem to be happening.

There are two problems here:

  • The API lies about what it is doing
  • There is no simple & obvious way to get the output of a view except directly into the response.