Wow, what an epic this is turning out to be! I think this might be of interest to people, so I'll post lots of code...
After quite a lot of web research, I created a class library called IdFilter. There's two files there:
1 - IdFilterModule.csCode:using System;
using System.Web;
namespace Company.Utilities
{
public class IdFilterModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication app)
{
app.ReleaseRequestState += new EventHandler(InstallResponseFilter);
}
private void InstallResponseFilter(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
if (response.ContentType == "text/html")
{
response.Filter = new IdFilter(response.Filter);
}
}
}
}
2 - IdFilter.csCode:using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
namespace Company.Utilities
{
public class IdFilter : Stream
{
Stream responseStream;
StringBuilder responseHtml;
long position;
public IdFilter(Stream inputStream)
{
responseStream = inputStream;
responseHtml = new StringBuilder();
}
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()
{
responseStream.Close();
}
public override void Flush()
{
responseStream.Flush();
}
public override long Length
{
get
{
return 0;
}
}
public override long Position
{
get
{
return position;
}
set
{
position = value;
}
}
public override long Seek(long offset, SeekOrigin origin)
{
return responseStream.Seek(offset, origin);
}
public override void SetLength(long length)
{
responseStream.SetLength(length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return responseStream.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch(strBuffer))
{
responseHtml.Append(strBuffer);
}
else
{
responseHtml.Append(strBuffer);
string finalHtml = responseHtml.ToString();
finalHtml = finalHtml.Replace("id=\"__VIEWSTATE\"", "id=\"VIEWSTATE\"");
finalHtml = finalHtml.Replace("id=\"__EVENTARGUMENT\"", "id=\"\"EVENTARGUMENT");
finalHtml = finalHtml.Replace("id=\"\"__EVENTTARGET", "id=\"\"EVENTTARGET");
finalHtml = finalHtml.Replace(" />", ">");
finalHtml = finalHtml.Replace(" />", ">");
finalHtml = finalHtml.Replace("/>", ">");
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);
responseStream.Write(data, 0, data.Length);
}
}
}
}
Yup, it really is that horrible :)
Building that project gives me a .dll I can copy to bin and then reference in web.config as follows:
Code:<httpModules>
<add name="IdFilterModule" type="Company.Utilities.IdFilterModule, IdFilter" />
</httpModules>
I created a test website project, incorporated the dll, set web.config and hey presto! My IDs were back to normal with underscores removed.
But when I copied this over to my umbraco site I was disheartened to see that there was no effect :(
I wonder if there is a glaring silly error on my part I need to address? I don't mind looking stupid if the fix is a 30-second one ;) Any ideas why it works in a normal website but not in umbraco?
Richard
2 * 3 * 3 * 37 : The prime factorisation of The Beast.