Hi,
Why does request filters in HttpModule only get triggered when the content type is x-www-form-urlencoded? A code example can be seen below.
Kind regards, Jesper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace MyWebApplication
{
public class MyHttpModule: IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += Application_BeginRequest;
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = application.Context;
context.Request.Filter = new MyRequestFilter(context.Request.Filter, context.Request.ContentEncoding);
var _ = context.Request.Form; // To trigger the evaluation of the inputstream
}
public void Dispose() { }
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyWebApplication
{
class MyRequestFilter: Stream
{
private MemoryStream ms;
private Stream _stream;
private Encoding _encoding;
public RequestFilter(Stream stream, Encoding encoding)
{
_stream = stream;
_encoding = encoding;
}
public override int Read(byte[] buffer, int offset, int count)
{
if (ms == null)
{
var sr = new StreamReader(_stream, _encoding);
string content = sr.ReadToEnd();
content = content.ToUpper();
Byte[] bytes = _encoding.GetBytes(content);
ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
ms.Seek(0, SeekOrigin.Begin);
}
return ms.Read(buffer, offset, count);
}
// rest of the overrides
// ...
}
}