DotNetNuke Response.End() Doesn’t End

screamOK.  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);

    }

}

 

Related Post

One Response to “DotNetNuke Response.End() Doesn’t End”

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear