U4-1599 - [MVC] ChangeContentType doesn't work in macros rendered from a View

Created by Tom Fulton 29 Jan 2013, 23:24:29 Updated by Tom Fulton 22 Jul 2013, 15:45:58

Is duplicated by: U4-2452

Relates to: U4-1537

Relates to: U4-2452

To render an RSS feed, we used to create XSLT macros and used this helper method to set the content-type to text/xml:

<xsl:value-of select="umbraco.library:ChangeContentType('text/xml')"/> If you attempt to render this macro from an MVC View, it renders fine but the content type is text/html. It works fine if you render from a masterpage instead of a view.

Is there a new way to achieve this from a view, or is it possible to make the ChangeContentType helper work in an MVC environment?

As workarounds, I've tried adding this to the view with no luck:

@{ Layout = null; Response.ContentType = "text/xml"; } I did find you can set the content type from a SurfaceController like so: public ActionResult Rss() { UmbracoContext.HttpContext.Response.Clear(); UmbracoContext.HttpContext.Response.ContentType = "text/xml"; return Content(someXmlString); } .. but it seems you can't render a macro from a SurfaceController (PageId is null)


Jeroen Breuer 30 Jan 2013, 08:11:17

Which PageId is null in the SurfaceController? The following does work: System.Web.HttpContext.Current.Items["pageID"]

Tom Fulton 30 Jan 2013, 16:58:41

Not sure, the exception was thrown from Umbraco.RenderMacro called from the SurfaceController:

System.InvalidOperationException: Cannot render a macro when UmbracoContext.PageId is null. at Umbraco.Web.UmbracoHelper.RenderMacro(String alias, IDictionary`2 parameters) My workaround for now is to just convert this template to a masterpage

Sebastiaan Janssen 18 Jul 2013, 10:07:37

Shannon, I'm assuming something sets the ContentType in the MVC pipeline, but I can't really find anything after searching on Response.ContentType. Any ideas?

Shannon Deminick 19 Jul 2013, 02:36:14

You can definitely set the ContentType result from a view like you've done (or in your controller action).

@{ Response.ContentType = "text/xml"; }

I've fixed the issue of the ContentType not being changed from the XSLT extension in rev: 6a9532f10ada80e84470e347d5f1ad01bdaf56bb

Also, can you please explain how you are attempting to do this in your SurfaceController to generate that error? As a ChildAction or some other way ? I've tested executing this xslt macro from a ChildAction of a SurfaceController and it works so the error you were getting has probably been fixed in later versions than you are working in.

Tom Fulton 22 Jul 2013, 15:45:58

I just tested again in 6.1.2 and changing the content type in the view/controller seems to work just fine now. Must have been an earlier/beta version it wasn't working on. Thanks!

I don't recall how I was trying to render a macro from the SurfaceController, but I'll post again if I can dig that up.

Priority: Normal

Type: Bug

State: Fixed

Assignee: Shannon Deminick

Difficulty: Normal

Category: Architecture

Backwards Compatible: True

Fix Submitted:

Affected versions: 4.11.3

Due in version: 6.1.3


Story Points: