J.Doe
J.Doe

Reputation: 155

Catching asp.net mvc all the responses. What are the options?

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?

Upvotes: 0

Views: 637

Answers (1)

rahulaga-msft
rahulaga-msft

Reputation: 4164

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>

Upvotes: 1

Related Questions