0

I need to catch all the responses of the app. How can I do it?

With regards to the incoming requests, I can work with the HttpModule, but I need the responses.

I could create the filters, but this will also require me to assign the filter attribute to each new controller and this is what I want to avoid.

Is there another way to do it?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
J.Doe
  • 155
  • 2
  • 7
  • 3
    It's unclear to me what you are asking. Can you please edit your question and provide more context and an [MCVE] – maccettura Feb 22 '18 at 14:53
  • https://stackoverflow.com/questions/1038466/logging-raw-http-request-response-in-asp-net-mvc-iis7#1792864 – stuartd Feb 22 '18 at 14:58
  • I've used a filter to catch all input and output for logging. You can configure it when you set up MVC so you don't need to add attributes to your controllers. Read more here https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters – Hans Kilian Feb 22 '18 at 14:58

1 Answers1

1

Assuming your application is not on ASP.NET Core, you can create module to subscribe to end request. Here you can capture response.

public class CaptureResponseModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.EndRequest += (o, s) =>
            {
                var response = HttpContext.Current.Response;
                /*capture response for logging/tracing etc. */
            };
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }

You need to register this module in your web.config file, something like :

 <system.web>
   <httpModules><add name="CaptureResponseModule " type="CaptureResponseModule"/></httpModules>
</system.web>
rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44