简体   繁体   English

OnActionExecuted不会将响应返回给webpi中的客户端

[英]OnActionExecuted doesn't return response to client in webpi

I want to log all requests (including response) sent to api controller. 我想记录所有发送到api控制器的请求(包括响应)。

public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        //my logic to get request and save in logs.
        var jsonBody = Utility.GetRequestJSON(actionContext);
        Utility.SaveLogAsync(jsonBody);
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionContext)
        {
            //my logic to get response and save in logs.
            string responseReturned = GetBodyFromRequest(actionContext);

            Utility.SaveLogAsync(responseReturned); //data is logged successfully but no response is returned to client.
            base.OnActionExecuted(actionContext);
        }
}

Here is my api controller: 这是我的api控制器:

[LogActionFilter]
    public class ServiceController : ApiController
    {
        [AcceptVerbs("GET", "POST")]
        [HttpGet]
        [HttpPost]
        public object Login()        
        {
            var jsonBody = Utility.GetRequestJSON(HttpContext.Current);
            AccountService _accountService = new AccountService(); 
            return _accountService.Login(jsonBody, Request.GetClientIP());
        }
    }

If I comment OnActionExecuted method then response is returned fine. 如果我评论OnActionExecuted方法,则响应返回正确。 But with OnActionExecuted method no response is returned. 但是使用OnActionExecuted方法不会返回任何响应。

I was reading response as: 我读的答复是:

string data;
            using (var stream = context.Response.Content.ReadAsStreamAsync().Result)
            {
                if (stream.CanSeek)
                {
                    stream.Position = 0;
                }

                data = context.Response.Content.ReadAsStringAsync().Result;
            }

Response wasn't returned to client because using dispose stream. 由于使用处置流,因此未将响应返回给客户端。
It worked after removing using : using删除后,它可以工作:

string data;
            var stream = context.Response.Content.ReadAsStreamAsync().Result;

                if (stream.CanSeek)
                {
                    stream.Position = 0;
                }
                data = context.Response.Content.ReadAsStringAsync().Result;

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

相关问题 即使OnActionExecuting返回了响应,也会触发OnActionExecuted - Fire OnActionExecuted even if response is returned from OnActionExecuting 如何读取和更改响应的主体到覆盖控制器的OnActionExecuted方法 - How to read and change body of Response into overided OnActionExecuted method of Controller 如果客户端向不存在的Web API资源发出DELETE,应该返回什么 - What should return if a client issues a DELETE to a web API resource which doesn't exist WebException 响应未返回完整响应 - WebException response didn't return the full response 在WebApi的ActionFilterAttribute的OnActionExecuted方法中修改HttpContent(actionExecutedContext.Response.Content) - Modify HttpContent (actionExecutedContext.Response.Content) in OnActionExecuted method of WebApi's ActionFilterAttribute Web Api不返回异常 - Web Api doesn't return exceptions 未映射的值返回不正确 - not mapped value doesn't return correct Angular Service没有返回值 - Angular Service doesn't return value 为什么我在客户端上看不到响应 class 的属性? - Why I don't see properties of the response class on the client? 如果遇到未处理的异常,Owin不会发送任何响应 - Owin doesn't send any response if an unhandled exception is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM