public async Task ExecuteResultAsync_WithValidContext_Passes()
        {
            // The ActionContext will hold the copy result
            var ac = new Microsoft.AspNetCore.Mvc.ActionContext
            {
                HttpContext = new DefaultHttpContext(),
            };

            // Execute
            await httpResponseMessageResult.ExecuteResultAsync(ac);

            var res = ac.HttpContext.Response;

            Assert.AreEqual(200, res.StatusCode);

            var responseFeature = ac.HttpContext.Features.Get <IHttpResponseFeature>();

            Assert.AreEqual(Reason, responseFeature.ReasonPhrase);

            res.Headers.TryGetValue("my-custom-header", out StringValues headerVal);
            Assert.AreEqual(httpResponseMessage.Headers.GetValues("my-custom-header"), headerVal);

            var val = httpResponseMessage.Content.Headers.GetValues("my-custom-content-header");

            res.Headers.TryGetValue("my-custom-content-header", out StringValues contentHeaderVal);
            Assert.AreEqual(val, contentHeaderVal);
        }
        public void ExecuteResultAsync_WithActionContextNoHttpContext_Throws()
        {
            // The ActionContext that SHOULD hold the copy result
            var ac = new Microsoft.AspNetCore.Mvc.ActionContext();

            // Execute
            Assert.ThrowsAsync(
                Is.TypeOf <ArgumentNullException>()
                .And.Message.EqualTo($"Response message can not be null (Parameter 'response')"),
                async() => await httpResponseMessageResult.ExecuteResultAsync(ac));
        }
        //
        // Summary:
        //     Executes the result operation of the action method synchronously. This method
        //     is called by MVC to process the result of an action method.
        //
        // Parameters:
        //   context:
        //     The context in which the result is executed. The context information includes
        //     information about the action that was executed and request information.
        public override void ExecuteResult(Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            context.HttpContext.Response.ContentType = MapMime(this.ThumbnailMime);
            // context.HttpContext.Response.ContentLength = 1024;
            ResizeImage(this.SourceStream, context.HttpContext.Response.Body
                        , this.MaxWidth, this.MaxHeight, this.ThumbnailMime
                        );

            if (this.m_Dispose && this.SourceStream != null)
            {
                this.SourceStream.Dispose();
            }
        }
示例#4
0
        } // End Sub ExecuteResult

        public async override Task ExecuteResultAsync(
            Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            ExecuteResult(context);

            //byte[] buffer = new byte[4096];
            //int read = 0;
            //while ((read = await this.ImageStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
            //{
            //    // response.OutputStream.Write(buffer, 0, read);
            //    await response.Body.WriteAsync(buffer, 0, read);
            //} // Whend

            await Task.CompletedTask;
        } // End Sub ExecuteResultAsync
示例#5
0
        // services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        // services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

        public MyImageTagHelper(
            Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment,
            Microsoft.Extensions.Caching.Memory.IMemoryCache cache,
            Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor,
            Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor actionContextAccessor,
            // For base constructor
            System.Text.Encodings.Web.HtmlEncoder htmlEncoder,
            Microsoft.AspNetCore.Mvc.Routing.IUrlHelperFactory urlHelperFactory
            )
            : base(urlHelperFactory, htmlEncoder)
        {
            this.HostingEnvironment = hostingEnvironment;
            this.Cache         = cache;
            this.HttpContext   = httpContextAccessor.HttpContext;
            this.ActionContext = actionContextAccessor.ActionContext;
        }
示例#6
0
        public override void ExecuteResult(Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response;

            //if (this.m_data == null)
            //{
            //    response.StatusCode = StatusCodes.Status500InternalServerError;
            //    return;
            //}

            byte[] data = this.m_enc.GetBytes(this.m_text);

            response.StatusCode    = StatusCodes.Status200OK;
            response.ContentLength = data.Length;
            response.ContentType   = this.m_MimeType + "; charset=" + this.Encoding.WebName;
            response.Body.Write(data, 0, data.Length);
        }
示例#7
0
        public override void ExecuteResult(Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response;

            //if (this.m_data == null)
            //{
            //    response.StatusCode = StatusCodes.Status500InternalServerError;
            //    return;
            //}

            response.StatusCode  = StatusCodes.Status200OK;
            response.ContentType = "application/javascript; charset=utf-8";

            Services.JsonSerializer ser = (Services.JsonSerializer)context.HttpContext
                                          .RequestServices.GetService(typeof(Services.JsonSerializer));
            ser.SerializeJsonp(context.HttpContext.Response.Body, "callback"
                               , this.m_data, this.m_prettyPrint);
        }
示例#8
0
        } // End Constructor

        async System.Threading.Tasks.Task Microsoft.AspNetCore.Mvc.IActionResult.ExecuteResultAsync(
            Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            if (context == null)
            {
                throw new System.ArgumentNullException("context");
            }

            if (XmlRequestBehavior == XmlRequestBehavior_t.DenyGet && string.Equals(context.HttpContext.Request.Method, "GET", System.StringComparison.OrdinalIgnoreCase))
            {
                throw new System.InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
            }

            Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response;

            // https://stackoverflow.com/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean

            if (this.m_sql == null)
            {
                response.StatusCode  = 500;
                response.ContentType = this.ContentType + "; charset=" + this.ContentEncoding.WebName;
                using (System.IO.StreamWriter output = new System.IO.StreamWriter(response.Body, this.ContentEncoding))
                {
                    await output.WriteAsync("{ error: true, msg: \"SQL-command is NULL or empty\"}");
                }

                return;
            } // End if (this.m_sql == null)


            using (System.Data.Common.DbConnection con = this.m_factory.Connection)
            {
                await OnlineYournal.SqlServiceXmlHelper.AnyDataReaderToXml(
                    con
                    , this.m_sql
                    , this.m_parameters
                    , "dbo"
                    , "T_BlogPost"
                    , this.ContentEncoding
                    , this.m_renderType
                    , context.HttpContext
                    );
            } // End Using con
        }     // End Task ExecuteResultAsync
示例#9
0
        } // End Constructor

        public override void ExecuteResult(Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            if (context == null)
            {
                throw new System.ArgumentNullException(nameof(context));
            }

            ImageHandler imageHandler = (ImageHandler)context.HttpContext.RequestServices
                                        .GetService(typeof(ImageHandler));

            Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response;

            if (!imageHandler.ImageFormatSupported(this.m_imageFormat))
            {
                response.StatusCode = 500;
                throw new System.NotSupportedException("Image-format is not supported");
            }

            response.ContentType = this.ImageFormat.MimeType();

            // Fsck, this is not the length of the resized image...
            // response.ContentLength = this.m_stream.Length;

            imageHandler.ResizeImage(this.m_stream, response.Body, SaveFormat.Png, this.m_maxSize);

            //byte[] buffer = new byte[4096];
            //int read = 0;
            //while ((read = this.ImageStream.Read(buffer, 0, buffer.Length)) > 0)
            //{
            //    // response.OutputStream.Write(buffer, 0, read);
            //    response.Body.Write(buffer, 0, read);
            //} // Whend

            // response.End();
            if (this.m_dispose)
            {
                this.m_stream?.Dispose();
            }
        } // End Sub ExecuteResult
        public async Task ExecuteResultAsync_IgnoresTransferEncoding_Passes()
        {
            // The ActionContext will hold the copy result
            var ac = new Microsoft.AspNetCore.Mvc.ActionContext
            {
                HttpContext = new DefaultHttpContext(),
            };

            // Set transfer encoding
            httpResponseMessage.Headers.TransferEncodingChunked = true;

            // recreate result object with updated message.
            httpResponseMessageResult = new HttpResponseMessageResult(httpResponseMessage);

            // Execute
            await httpResponseMessageResult.ExecuteResultAsync(ac);

            var res = ac.HttpContext.Response;

            Assert.AreEqual(200, res.StatusCode);

            var responseFeature = ac.HttpContext.Features.Get <IHttpResponseFeature>();

            Assert.AreEqual(Reason, responseFeature.ReasonPhrase);

            res.Headers.TryGetValue("my-custom-header", out StringValues headerVal);
            Assert.AreEqual(httpResponseMessage.Headers.GetValues("my-custom-header"), headerVal);

            var val = httpResponseMessage.Content.Headers.GetValues("my-custom-content-header");

            res.Headers.TryGetValue("my-custom-content-header", out StringValues contentHeaderVal);
            Assert.AreEqual(val, contentHeaderVal);

            // Verify transfer encoding was ignored
            res.Headers.TryGetValue("Transfer-Encoding", out StringValues transferEnc);
            Assert.IsTrue(transferEnc.Count == 0);
        }
        //
        // Summary:
        //     Executes the result operation of the action method asynchronously. This method
        //     is called by MVC to process the result of an action method. The default implementation
        //     of this method calls the Microsoft.AspNetCore.Mvc.ActionResult.ExecuteResult(Microsoft.AspNetCore.Mvc.ActionContext)
        //     method and returns a completed task.
        //
        // Parameters:
        //   context:
        //     The context in which the result is executed. The context information includes
        //     information about the action that was executed and request information.
        //
        // Returns:
        //     A task that represents the asynchronous execute operation.
        public /* async */ override System.Threading.Tasks.Task ExecuteResultAsync(
            Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            if (this.FileDownloadName != null)
            {
                context.HttpContext.Response.Headers.Add("Content-Disposition"
                                                         , new[] { "attachment; filename=" + this.FileDownloadName }
                                                         );
            }

            context.HttpContext.Response.ContentType = MapMime(this.ThumbnailMime);
            // context.HttpContext.Response.ContentLength = 1024;

            ResizeImage(this.SourceStream, context.HttpContext.Response.Body,
                        this.MaxWidth, this.MaxHeight, this.ThumbnailMime
                        );

            if (this.m_Dispose && this.SourceStream != null)
            {
                this.SourceStream.Dispose();
            }

            return(System.Threading.Tasks.Task.FromResult(0));

            /*
             * Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response;
             * response.ContentType = MapMime(this.ThumbnailMime);
             * context.HttpContext.Response.Headers.Add("Content-Disposition"
             *  , new[] { "attachment; filename=" + "FileDownloadName" });
             *
             * using (System.IO.FileStream fs = new System.IO.FileStream("filePath", System.IO.FileMode.Open))
             * {
             *  await fs.CopyToAsync(context.HttpContext.Response.Body);
             * }
             */
        }
 public void OnBeforeActionMethod(Microsoft.AspNetCore.Mvc.ActionContext actionContext, System.Collections.Generic.IDictionary <string, object> actionArguments, object controller)
 {
     _onActionMethodScope.Value = TimelineScope.Create("ActionMethod", TimelineEventCategory.AspNetCoreMvcAction, actionContext.ActionDescriptor.DisplayName);
 }
示例#13
0
 public async override Task ExecuteResultAsync(
     Microsoft.AspNetCore.Mvc.ActionContext context)
 {
     ExecuteResult(context);
     await Task.CompletedTask;
 } // End Sub ExecuteResultAsync
示例#14
0
        async System.Threading.Tasks.Task Microsoft.AspNetCore.Mvc.IActionResult.ExecuteResultAsync(
            Microsoft.AspNetCore.Mvc.ActionContext context)
        {
            if (context == null)
            {
                throw new System.ArgumentNullException("context");
            }

            if (JsonRequestBehavior == JsonRequestBehavior_t.DenyGet &&
                string.Equals(context.HttpContext.Request.Method, "GET", System.StringComparison.OrdinalIgnoreCase))
            {
                throw new System.InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
            }

            Microsoft.AspNetCore.Http.HttpResponse response = context.HttpContext.Response;
            // https://stackoverflow.com/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean
            response.ContentType = this.ContentType + "; charset=" + this.ContentEncoding.WebName;

            if (Data == null)
            {
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(response.Body, this.ContentEncoding))
                {
                    // await writer.WriteLineAsync("null");
                    await writer.WriteLineAsync("{}");
                } // End Using writer

                return;
            } // End if (Data == null)


#if false
            Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings =
                new Newtonsoft.Json.JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };


            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(response.Body, this.ContentEncoding))
            {
                using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(writer))
                {
                    Newtonsoft.Json.JsonSerializer ser = Newtonsoft.Json.JsonSerializer.Create(jsonSerializerSettings);

                    ser.Serialize(jsonWriter, Data);
                    await jsonWriter.FlushAsync();
                } // End Using jsonWriter

                await writer.FlushAsync();
            } // End Using writer
#endif


            System.Text.Json.JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions()
            {
                IncludeFields        = true,
                WriteIndented        = true,
                PropertyNamingPolicy = this.NamingPolicy
                                       // PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase
            };

            await System.Text.Json.JsonSerializer.SerializeAsync(response.Body, Data, options);
        } // End Task ExecuteResultAsync
 public ViewLocationExpanderContext(Microsoft.AspNetCore.Mvc.ActionContext actionContext, string viewName, string controllerName, string areaName, string pageName, bool isMainPage)
 {
 }
 public static string GetNormalizedRouteValue(Microsoft.AspNetCore.Mvc.ActionContext context, string key)
 {
     throw null;
 }
 public Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult FindView(Microsoft.AspNetCore.Mvc.ActionContext context, string viewName, bool isMainPage)
 {
     throw null;
 }
 public Microsoft.AspNetCore.Mvc.Razor.RazorPageResult FindPage(Microsoft.AspNetCore.Mvc.ActionContext context, string pageName)
 {
     throw null;
 }
 public void OnBeforeActionResult(Microsoft.AspNetCore.Mvc.ActionContext actionContext, Microsoft.AspNetCore.Mvc.IActionResult result)
 {
     _onActionResultScope.Value = TimelineScope.Create("ActionResult", TimelineEventCategory.AspNetCoreMvcResult);
 }