简体   繁体   English

捕获asp.net mvc的所有响应。 有什么选择?

[英]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. 关于传入的请求,我可以使用HttpModule ,但是我需要响应。

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? 还有另一种方法吗?

Assuming your application is not on ASP.NET Core, you can create module to subscribe to end request. 假设您的应用程序不在ASP.NET Core上,则可以创建模块以订阅最终请求。 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 : 您需要在web.config文件中注册该模块,例如:

 <system.web>
   <httpModules><add name="CaptureResponseModule " type="CaptureResponseModule"/></httpModules>
</system.web>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM