【发布时间】:2014-04-12 16:48:09
【问题描述】:
我希望将自定义委托处理程序添加到我的 asp.net web api。这个处理程序的目的是检查我的请求的内容,并在它们继续在请求管道中之前验证它们的某些条件......我还想在这个入口点注销请求。
但是,在管道中传递的 request.content 是 system.web.http.webhost.httpcontrollerhandler.lazystreamcontent - 在 MSDN 中查找这种类型并没有揭示如何反序列化或查看请求内容 - 当我尝试从流中读取内容,我留下了一个空字符串。见例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace Haxy.Pencilthis.Api.MVC.Handlers
{
public class ContentValidationHandler : DelegatingHandler
{
private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
Stream strea = new MemoryStream();
request.Content.CopyToAsync(strea);
StreamReader reader = new StreamReader(strea);
String res = reader.ReadToEnd();
Log.Info("request content: " + res);
return base.SendAsync(request, cancellationToken);
//
}
}
}
【问题讨论】:
标签: c# logging asp.net-web-api log4net