question

JesperLundin-7440 avatar image
0 Votes"
JesperLundin-7440 asked JesperLundin-7440 commented

Request filter in HttpModule only works for form data

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
         // ...
     }
 }

dotnet-csharpdotnet-aspnet-general
· 18
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JesperLundin-7440,
Do you have register post method in HttpModule? Could you post web.config to us?
Best regards,
Yijing Sun

0 Votes 0 ·

Here is the webconfig. I had to rename the extension to .txt to be able to upload it.

100482-web.txt

I added logging to the project and I can see that the Read method in the request filter is executed. Also, post works when the content type is x-www-form-urlencoded but not for example json. Here are some examples from Postman. I am using an Web API which responds with the same content as the request.

Here is an example with form data
100455-2021-05-28-09-55-46-window.png

Here is an example with json
100495-2021-05-28-09-56-16-window.png



0 Votes 0 ·

Hi @JesperLundin-7440 ,
I think,you need to reset the position of the stream before reading and use Json.NET to deserialize to an object.
Best regards,
Yijing Sun

0 Votes 0 ·
Show more comments

0 Answers