Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,026 | Comments: 44,842

filter by tags archive

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?


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.



That's weird, when you supply your own textwriter the render method of the view should write to it.

It's used in testing views as well.

In my testing code there is:

        MyTextView view = new MyTextView (templatePath);

        StringWriter writer = new StringWriter();

        view.Render(null, writer);

        string rendered = writer.ToString();

And it works correctly, at least inside a test case.

Ayende Rahien

Try to execute my code.

You are going through a different code path.



The WebFormView doesn't do anything with the writer parameter, it just swallows it....

that really a bug... even tho I think it cannot be changed since the webviewengine is based on the webform model.

One more reason not to use the WebFormViewEngine :)


This is one of them tough decisions we had due to the nature of the framework we're building on. It's less than ideal, but is for the greater good eventually.

Yes, the WebFormViewEngine ignores the writer parameter. We exposed the parameter in the underlying API to encourage other view engine implementors to do the right thing and write to the writer as they render the output rather than creating one big string that they write out at the end. In part, this was to make sure that RenderPartial would work with other view engines.

Unfortunately, we can't change our own view engine to do this same thing as that would require changes to core bits of the Page class etc... MVC is built on top of ASP.NET core and we can't make changes to the core for MVC right now. Maybe when the next major framework release comes out, we can actually fix this.

However, since the WebFormViewEngine already streams output to the writer rather than generating one big string, it doesn't suffer the key problem we wanted to solve.


@Haacked: maybe a compromise?

I mean, when the writer parameter is the Response then you do nothing and go like now, so no possible performances hits.

When the writer parameter is a custom one you change the response output stream calling the HttpResponse's private method "SwitchWriter" (using reflection) to supply the custom writer.

Ayende Rahien


This means that I cannot get the view into a string.

That is a PROBLEM.

This is so because this is an incredibly common thing to do when you are playing around with composable apps and manipulating views.

At the very least, this should throw an exception about this being not supported.

Or, much better (from my point of view), is to get the framework to fake the pipeline in order to support it.

Frans Bouma

I might be wrong, but isn't this still in beta? If so, why are you doing production work on this?



use the Spark view engine :)

Steve Wagner

I am agree with Ayende. When its not possible to do it, it should throw a exception instead of letting me standing in the rain.


It has taken me a long time to find this answer.

Wow what a waste of time (+4 hours of hacking and searching).

I wish there was better documentation for ASP.net (I know it's beta - but i'm impatient :) )


I spent a few hours on the same problem (see http://forums.asp.net/t/1315284.aspx) and it's pretty disappointing to hear it cannot be fixed for RTM :-(

@Haacked: Isn't there a way to "hack" it with some output caching? Something like BlockRenderer (see the link) does?

Kevin Zink

Hey Everyone,

I put together a rough framework which allows you to render views to a string from a controller method in MVC Beta.

Additionally, I also put together a Rails-like RJS javascript generating framework for MVC Beta.

Check it out at www.brightmix.com/.../how-to-renderpartial-to-s... and let me know what you think.


try this

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Technical observations from my wife (3):
    13 Nov 2015 - Production issues
  2. Production postmortem (13):
    13 Nov 2015 - The case of the “it is slow on that machine (only)”
  3. Speaking (5):
    09 Nov 2015 - Community talk in Kiev, Ukraine–What does it take to be a good developer
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats