Accessing the raw http request in MVC4

蓝咒 提交于 2019-12-06 00:12:55
Darin Dimitrov
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!