网页API行为过滤器的内容不能被读取 [英] Web API action filter content can't be read
问题描述
相关问题:<一href=\"http://stackoverflow.com/questions/18256817/web-api-action-parameter-is-intermittently-null\">Web API操作参数是间歇性空和<一个href=\"http://social.msdn.microsoft.com/Forums/vstudio/en-US/25753b53-95b3-4252-b034-7e086341ad20/web-api-action-parameter-is-intermittently-null\">http://social.msdn.microsoft.com/Forums/vstudio/en-US/25753b53-95b3-4252-b034-7e086341ad20/web-api-action-parameter-is-intermittently-null
你好!
我要创建在ASP.Net MVC的WebAPI 4 ActionFilterAttribute这样我就可以申请该属性在控制器的操作方法,我们需要一个令牌验证之前执行它下面的code:
I'm creating a ActionFilterAttribute in ASP.Net MVC WebAPI 4 so I can apply the attribute in action methods at the controller that we need validation of a token before execute it as the following code:
public class TokenValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext filterContext)
{
//Tried this way
var result = string.Empty;
filterContext.Request.Content.ReadAsStringAsync().ContinueWith((r)=> content = r.Result);
//And this
var result = filterContext.Request.Content.ReadAsStringAsync().Result;
//And this
var bytes = await request.Content.ReadAsByteArrayAsync().Result;
var str = System.Text.Encoding.UTF8.GetString(bytes);
//omit the other code that use this string below here for simplicity
}
}
我想阅读的内容为字符串。试图在这个code表示,所有这些都返回空的3种方式。我知道,在我的WebAPI可以只读取一次请求的主体内容,所以我评论了code一切,并试图让它运行,看看我得到的结果。问题的关键是,在客户端和连提琴手,报告请求的内容长度的315。相同尺寸越来越服务器上的内容头以及但是,当我们试图阅读的内容,它是空的。
I'm trying to read the content as string. Tried the 3 ways as stated in this code, and all of them return empty. I know that in WebApi I can read only once the body content of the request, so I'm commenting everything else in the code, and trying to get it run to see if I'm getting a result. The point is, the client and even the Fiddler, reports the 315 of the content length of the request. The same size is getting on the server content header as well but, when we try read the content, it is empty.
如果我删除属性,并提出同样的要求,控制器被称为好,和JSON的反序列化发生无措。如果我把属性,我得到的是从内容的空字符串。它总是会发生。不间歇的相关问题的状态。
If I remove the attribute and make the same request, the controller is called well, and the deserialization of Json happens flawless. If I put the attribute, all I get is a empty string from the content. It happens ALWAYS. Not intermittent as the related questions state.
我是什么做错了吗?请记住,我使用的,而不是DelegatingHandler ActionFilter因为只有选定的操作之前需要令牌验证来执行。
What am I doing wrong? Keep in mind that I'm using ActionFilter instead of DelegatingHandler because only selected actions requires the token validation prior to execution.
感谢您的帮助!我真的AP preciate它。
Thanks for help! I really appreciate it.
问候...
Gutemberg
Gutemberg
推荐答案
默认情况下,虚拟主机(IIS)情况下的缓冲政策是传入的请求的流总是缓冲。你可以看看 System.Web.Http.WebHost.WebHostBufferPolicySelector
。现在,当你想通,网页API的格式化将消耗该流并不会尝试倒带回来。这是有意的,因为人们可以改变缓冲策略以使进入的请求的信息流被非缓冲在这种情况下,再卷绕会失败。
By default the buffer policy for Web Host(IIS) scenarios is that the incoming request's stream is always buffered. You can take a look at System.Web.Http.WebHost.WebHostBufferPolicySelector
. Now as you have figured, Web Api's formatters will consume the stream and will not try to rewind it back. This is on purpose because one could change the buffer policy to make the incoming request's stream to be non-buffered in which case the rewinding would fail.
所以你的情况,因为你知道该请求将被永远缓存,你可以得到的输入流中保持像下面和倒带。
So in your case, since you know that the request is going to be always buffered, you could get hold of the incoming stream like below and rewind it.
Stream reqStream = await request.Content.ReadAsStreamAsync();
if(reqStream.CanSeek)
{
reqStream.Position = 0;
}
//now try to read the content as string
string data = await request.Content.ReadAsStringAsync();
这篇关于网页API行为过滤器的内容不能被读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!