Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

@

Posts: 5,947 | Comments: 44,540

filter by tags archive

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


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.

Comments

Simone

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.

Simone

OMG!!!!

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 :)

Haacked

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.

Simone

@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

Phil,

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?

Torkel

Ayende,

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.

Harry

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 :) )

Endif

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.

kk
kk

try this

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. RavenDB Sharding (2):
    21 May 2015 - Adding a new shard to an existing cluster, the easy way
  2. The RavenDB Comic Strip (2):
    20 May 2015 - Part II – a team in trouble!
  3. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  4. Interview question (2):
    30 Mar 2015 - fix the index
  5. Excerpts from the RavenDB Performance team report (20):
    20 Feb 2015 - Optimizing Compare – The circle of life (a post-mortem)
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats