protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            if (_configuration.SetNoCacheForAllResponses)
            {
                //Based on http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    NoCache        = true,
                    NoStore        = true,
                    MaxAge         = TimeSpan.Zero,
                    MustRevalidate = true
                };
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? _configuration.DefaultWrapResultAttribute;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            if (IsIgnoredUrl(request.RequestUri))
            {
                return;
            }

            object resultObject;

            if (!response.TryGetContentValue(out resultObject) || resultObject == null)
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content    = new ObjectContent <AjaxResponse>(
                    new AjaxResponse(),
                    _configuration.HttpConfiguration.Formatters.JsonFormatter
                    );
                return;
            }

            if (resultObject is AjaxResponseBase)
            {
                return;
            }

            response.Content = new ObjectContent <AjaxResponse>(
                new AjaxResponse(resultObject),
                _configuration.HttpConfiguration.Formatters.JsonFormatter
                );
        }
        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            if (_configuration.SetNoCacheForAllResponses)
            {
                //Based on http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    NoCache        = true,
                    NoStore        = true,
                    MaxAge         = TimeSpan.Zero,
                    MustRevalidate = true
                };
            }

            var absolutePath = request.RequestUri.AbsolutePath;

            if (_abpWebCommonModuleConfiguration.WrapResultFilters.HasFilterForWrapOnSuccess(absolutePath, out var wrapOnSuccess))
            {
                if (!wrapOnSuccess)
                {
                    return;
                }

                Wrap(response);
                return;
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? _configuration.DefaultWrapResultAttribute;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            if (IsIgnoredUrl(request.RequestUri))
            {
                return;
            }

            Wrap(response);
        }
Example #3
0
        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? DontWrapResultAttribute.Default;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            object resultObject;

            if (!response.TryGetContentValue(out resultObject))
            {
                return;
            }

            if (resultObject == null)
            {
                response.Content = new ObjectContent <AjaxResponse>(
                    new AjaxResponse(),
                    _webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
                    );
            }

            if (resultObject is AjaxResponse)
            {
                return;
            }

            response.Content = new ObjectContent <AjaxResponse>(
                new AjaxResponse(resultObject),
                _webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
                );
        }
        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            if (_configuration.SetNoCacheForAllResponses)
            {
                //Based on http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    NoCache        = true,
                    NoStore        = true,
                    MaxAge         = TimeSpan.Zero,
                    MustRevalidate = true
                };
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? _configuration.DefaultWrapResultAttribute;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            if (IsIgnoredUrl(request.RequestUri))
            {
                return;
            }

            // *King
            object resultObject;

            if (!response.TryGetContentValue(out resultObject) || resultObject == null)
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content    = new ObjectContent <RespData>(
                    new RespData(),
                    _configuration.HttpConfiguration.Formatters.JsonFormatter
                    );
                return;
            }

            if (resultObject is RespDataBase)
            {
                return;
            }

            // *King
            // Check returns type
            if (resultObject is HttpFileOutput)
            {
                var fileOutput = (HttpFileOutput)resultObject;
                if (!File.Exists(fileOutput.FilePath))
                {
                    response.StatusCode = HttpStatusCode.NotFound;
                    response.Content    = new StringContent("文件不存在,请重新确认文件Url");
                    return;
                }
                if (fileOutput.FileName.IsNullOrEmpty())
                {
                    fileOutput.FileName = Path.GetFileName(fileOutput.FilePath);
                }

                var fileInfo = File.Open(fileOutput.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                response.StatusCode = HttpStatusCode.OK;
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content = new CustomStreamContent(fileInfo, 1024 * 1024, () => {
                    if (fileOutput.IsTempFile)
                    {
                        File.Delete(fileOutput.FilePath);
                    }
                });
                response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = fileOutput.FileName;
                Logger.DebugFormat("开始传输,文件路径{0}", fileOutput.FilePath);

                return;
            }

            response.Content = new ObjectContent <RespData>(
                new RespData(resultObject),
                _configuration.HttpConfiguration.Formatters.JsonFormatter
                );
        }