Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DotNetNuke Response.End() Doesn’t End

DZone's Guide to

DotNetNuke Response.End() Doesn’t End

·
Free Resource
OK.  This one is just plain annoying!

For some reason that I’ve yet to be able to track down, Response.End() seems to have no impact at all under dotnetnuke.

I tried various hacks.  Including the one you’ve probably seen already about executing Response.End() twice.  No good.

Execute Response.Close() and then Response.End() … infinite loop!

But, I was able to come up with a solution that is reliable.

The specific issue I was trying to solve was that I wanted a Module to render an RSS feed.  Typically the way you’d do this is Response.Write() out the RSS feed information and then Response.End() so that no more information will render.  But something up the line in DNN seems to be trapping that so that the DNN page renders immediately after the RSS feed I rendered.  Aside from that rendering a lot more information than I had in mind, it also doesn’t work when you view the RSS feed in most browsers.

What I ended up doing to fix the problem is I created a Response Filter that strips off the extra information and returns just the RSS feed back to the client.

Here’s the basic code:

public class RssFilter : Stream
{
    readonly Stream m_responseStream;
    readonly StringBuilder m_html = new StringBuilder();

    public RssFilter(Stream inputStream)
    {
        m_responseStream = inputStream;
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return true; }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override void Close()
    {
        string tempResponse = m_html.ToString();
        tempResponse = tempResponse.Substring(0,
            tempResponse.IndexOf("</rss>",
            System.StringComparison.Ordinal) + 6);

        byte[] data = Encoding.UTF8
            .GetBytes(tempResponse);
        m_responseStream.Write(data, 0, data.Length);
        m_responseStream.Close();
    }

    public override void Flush()
    {
        m_responseStream.Flush();
    }

    public override long Length
    {
        get { return 0; }
    }

    public override long Position { get; set; }

    public override long
        Seek(long offset, SeekOrigin direction)
    {
        return m_responseStream.Seek(offset, direction);
     }

    public override void
        SetLength(long length)
    {
        m_responseStream
            .SetLength(length);
    }

    public override int Read
        (byte[] buffer, int offset, int count)
    {
        return m_responseStream
           .Read(buffer, offset, count);
    }

    public override void
        Write(byte[] buffer, int offset, int count)
    {
        var sBuffer = Encoding.UTF8
            .GetString(buffer, offset, count);
        m_html.Append(sBuffer);

    }

}
Topics:

Published at DZone with permission of Dave Bush, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}