访问在MVC4 HTTP请求的原始 [英] Accessing the raw http request in MVC4

查看:92
本文介绍了访问在MVC4 HTTP请求的原始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于访问的Htt prequest计算器上已经问题,但我一直没能找到一个我可以用呢。如果有人可以点我一个答案我将不胜感激。

我的问题是,我想修改,以充分请求记录添加尽可能以最简单的方式现有的应用程序。我知道有这可以通过HTTP模块以及IIS日志记录或使用log4net的等等等等,做,但我不希望使用任何这些技术,因为应用程序是联合国predictable所以我需要一个解决方案,不需要在所有的任何配置更改。

我已经加入到OnActionExecuted基地控制器。

 保护覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
{
    // TODO:从日志过滤器上下文请求....
    base.OnActionExecuted(filterContext);
}

在待办事项部分上面我想读的原始http请求,并将其保存到数据库文件。登录到数据库是很简单的,但什么是序列化或以其他方式转换HTTP请求作为字符串的最佳方式?

感谢您


解决方案

 保护覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
{
    VAR请求= filterContext.HttpContext.Request
    VAR的InputStream = request.InputStream;
    inputStream.Position = 0;
    使用(VAR读者=新的StreamReader(InputStream的))
    {
        串标头= request.Headers.ToString();
        串体= reader.ReadToEnd();
        字符串rawRequest =的String.Format(
            {0} {1} {1} {2},标题,Environment.NewLine,身体
        );
        // TODO:登录的rawRequest到数据库
    }
}

哦,如果你开始这样做,我希望你已经规划您将需要为您的服务器上购买额外的存储预算。如果你开始记录每个整个原始请求堂妹要求你会需要存储。

There are a lot of questions about accessing the HttpRequest on stackoverflow already, but I have not been able to find one that I can use yet. If somebody could point me to an answer I would be grateful.

My problem is that I want to modify an existing application in the simplest way possible in order to add in full request logging. I'm aware there this can be done with Http modules and IIS logging or using Log4net etc etc. But I don't want to use any of these techniques because the application is unpredictable so I need a solution that does not require any configuration changes at all.

I have added OnActionExecuted to a base controller.

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    // todo: log request from filter context ....
    base.OnActionExecuted(filterContext);
}

In the "todo" part above I wish to read the raw http request and save it to a database file. Logging to a database is simple enough but what is the best way to serialize or otherwise convert the http request as a string?

Thank you

解决方案

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    var request = filterContext.HttpContext.Request
    var inputStream = request.InputStream;
    inputStream.Position = 0;
    using (var reader = new StreamReader(inputStream))
    {
        string headers = request.Headers.ToString();
        string body = reader.ReadToEnd();
        string rawRequest = string.Format(
            "{0}{1}{1}{2}", headers, Environment.NewLine, body
        );
        // TODO: Log the rawRequest to your database
    }
}

Oh and if you start doing that, I hope you have already planned the budget you will need for purchasing additional storage on your servers. Coz if you start logging the entire raw request on each request you're gonna need storage.

这篇关于访问在MVC4 HTTP请求的原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆