In my app I need to be able to update several parts of the page with a single ajax request.
I have been using the BlockRenderer method for rendering several partial views. I then stick the rendered html into a JSON object for the javascript to insert into the page.
It appears RC1 breaks the BlockRenderer method. After rendering a partial view, the response stream must be flushed before you can retrieve the contents of the CapturingResponseFilter. It looks like Render() now resets the content type-- but since the response
is flushed the first time you render a view, the headers are sent, and when the second view is rendered, you get an exception when it tries to reset the content type.
Is this related to the comment in the release notes about RenderPartial no longer resetting the content type? How can I work around this? Thanks in advance.
I agree, and it seems like a common enough request/complaint. I have been hoping that there would be a good way to do this before the final release, but that's looking less & less likely. It only seems to be getting more difficult with each new release...
I have also considered switching to a new view engine, but it would require rewriting quite a bit of code, which I would obviously like to avoid.
The reason we reset the content type is that if you set the content type in the action method, when you render the view, the view would change the content type. This is due to the Page's ProcessRequest method which sets the content type. Hopefully we can
look at making changes to the core Page class in .NET 4, but we'll see. Sorry for the annoyance.
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
Thanks for the explanation Phil. Can you recommend any alternatives for capturing the rendered HTML from a partial view? If not, is there any other way to update several different components of the page in a single request? Unfortunately my app is pretty dependent
on this.
I've changed the last line of PartialRequest.Invoke to
SimpleWorkerRequest request = new SimpleWorkerRequest("dummy.html", null, new StringWriter(CultureInfo.CurrentCulture));
handler.ProcessRequest(new HttpContext(request));
and everything seems to be working again. Thoughts?
Edit: Just to clarify, I'm using BlockRenderer
this way.
brs
0 Points
18 Posts
BlockRenderer method broken in RC1?
Jan 27, 2009 11:50 PM|LINK
Hello,
In my app I need to be able to update several parts of the page with a single ajax request.
I have been using the BlockRenderer method for rendering several partial views. I then stick the rendered html into a JSON object for the javascript to insert into the page.
It appears RC1 breaks the BlockRenderer method. After rendering a partial view, the response stream must be flushed before you can retrieve the contents of the CapturingResponseFilter. It looks like Render() now resets the content type-- but since the response is flushed the first time you render a view, the headers are sent, and when the second view is rendered, you get an exception when it tries to reset the content type.
Is this related to the comment in the release notes about RenderPartial no longer resetting the content type? How can I work around this? Thanks in advance.
Nik Coughlin
Member
34 Points
16 Posts
Re: BlockRenderer method broken in RC1?
Jan 28, 2009 01:43 AM|LINK
I was going to transition to RC1 today too, glad I didn't.
I'm doing something vaguely similar, using a BlockRenderer so that I can do RenderPartialToString
We need to be able to render partials to strings! There are just too many things that it's useful for.
Either that or stop using the webform view. God knows I'm not terribly fond of it... but I like intellisense too much to change my view engine.
brs
0 Points
18 Posts
Re: BlockRenderer method broken in RC1?
Jan 28, 2009 04:17 PM|LINK
I agree, and it seems like a common enough request/complaint. I have been hoping that there would be a good way to do this before the final release, but that's looking less & less likely. It only seems to be getting more difficult with each new release...
I have also considered switching to a new view engine, but it would require rewriting quite a bit of code, which I would obviously like to avoid.
Haacked
Contributor
6901 Points
412 Posts
Re: BlockRenderer method broken in RC1?
Jan 28, 2009 04:32 PM|LINK
The reason we reset the content type is that if you set the content type in the action method, when you render the view, the view would change the content type. This is due to the Page's ProcessRequest method which sets the content type. Hopefully we can look at making changes to the core Page class in .NET 4, but we'll see. Sorry for the annoyance.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
brs
0 Points
18 Posts
Re: BlockRenderer method broken in RC1?
Jan 28, 2009 04:43 PM|LINK
Nik Coughlin
Member
34 Points
16 Posts
Re: BlockRenderer method broken in RC1?
Jan 28, 2009 07:47 PM|LINK
Here's a thread on Stackoverflow with several alternate methods
http://stackoverflow.com/questions/483091/render-a-view-as-a-string
brs
0 Points
18 Posts
Re: BlockRenderer method broken in RC1?
Jan 28, 2009 08:17 PM|LINK
Thanks, but the methods there are very similar to the block renderer, and have the same problem...
Herne
Member
10 Points
12 Posts
Re: BlockRenderer method broken in RC1?
Jan 29, 2009 03:56 PM|LINK
Does anyone have any alternative ideas?
(Or a pointer to an idea :) )
Herne
Member
10 Points
12 Posts
Re: BlockRenderer method broken in RC1?
Jan 30, 2009 08:31 AM|LINK
I've changed the last line of PartialRequest.Invoke to
SimpleWorkerRequest request = new SimpleWorkerRequest("dummy.html", null, new StringWriter(CultureInfo.CurrentCulture));
handler.ProcessRequest(new HttpContext(request));
and everything seems to be working again. Thoughts?
Edit: Just to clarify, I'm using BlockRenderer this way.
Nik Coughlin
Member
34 Points
16 Posts
Re: BlockRenderer method broken in RC1?
Feb 01, 2009 07:58 PM|LINK
Thanks for posting that Herne, that's extremely reassuring!