PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/Controls/RewriteFormHtmlTextWriter.cs

#
C# | 88 lines | 49 code | 8 blank | 31 comment | 9 complexity | 4f7204e45a544a44823ed59781623740 MD5 | raw file
 1namespace BlogEngine.Core.Web.Controls
 2{
 3    using System.IO;
 4    using System.Web;
 5    using System.Web.UI;
 6
 7    /// <summary>
 8    /// The RewriteFormHtmlTextWriter class implements Form action tag rewriting for rewritten pages 
 9    ///     on Mono.
10    /// </summary>
11    public class RewriteFormHtmlTextWriter : HtmlTextWriter
12    {
13        #region Constructors and Destructors
14
15        /// <summary>
16        /// Initializes a new instance of the <see cref="RewriteFormHtmlTextWriter"/> class.
17        /// </summary>
18        /// <param name="writer">
19        /// The writer.
20        /// </param>
21        public RewriteFormHtmlTextWriter(Html32TextWriter writer)
22            : base(writer)
23        {
24            this.InnerWriter = writer.InnerWriter;
25        }
26
27        /// <summary>
28        /// Initializes a new instance of the <see cref="RewriteFormHtmlTextWriter"/> class.
29        /// </summary>
30        /// <param name="writer">
31        /// The writer.
32        /// </param>
33        public RewriteFormHtmlTextWriter(TextWriter writer)
34            : base(writer)
35        {
36            this.InnerWriter = writer;
37        }
38
39        #endregion
40
41        #region Public Methods
42
43        /// <summary>
44        /// Writes the specified markup attribute and value to the output stream, and, if specified, writes the value encoded.
45        /// </summary>
46        /// <param name="name">
47        /// The markup attribute to write to the output stream.
48        /// </param>
49        /// <param name="value">
50        /// The value assigned to the attribute.
51        /// </param>
52        /// <param name="encode">
53        /// true to encode the attribute and its assigned value; otherwise, false.
54        /// </param>
55        public override void WriteAttribute(string name, string value, bool encode)
56        {
57            // Mono has issues identifying relative paths when the url is rewritten,
58            // so we need to place the full path in the form tag's action attribute
59            // or postbacks won't work in rewritten pages.
60            if (Utils.IsMono)
61            {
62                if (name == "action")
63                {
64                    if (HttpContext.Current.Items["ActionAlreadyWritten"] == null)
65                    {
66                        value = Utils.AbsoluteWebRoot + value;
67                        HttpContext.Current.Items["ActionAlreadyWritten"] = true;
68                    }
69                }
70            }
71            else
72            {
73                if (name == "action")
74                {
75                    if (HttpContext.Current.Items["ActionAlreadyWritten"] == null)
76                    {
77                        value = HttpContext.Current.Request.RawUrl;
78                        HttpContext.Current.Items["ActionAlreadyWritten"] = true;
79                    }
80                }
81            }
82
83            base.WriteAttribute(name, value, encode);
84        }
85
86        #endregion
87    }
88}