【问题标题】:ASP.NET CORE OnActionExecuting Not Firing RemotelyASP.NET CORE OnActionExecuting 不远程触发
【发布时间】:2019-12-11 06:08:20
【问题描述】:

我有一个 ASP.NET CORE WebApi 项目,它有一个基控制器类,它覆盖 OnActionExecuting 方法来执行工作(什么工作对这篇文章无关紧要)。

OnActionExecuting 方法在我在本地运行时触发(VS Pro 2017 - localhost - IIS Express),但在我将具有相同代码的构建发布到服务器环境时无法触发(Windows Server 2016 Standard - IIS 10.0.14393.0 )。

任何想法为什么 OnActionExecuting 方法在 localhost 上被调用但无法在运行 IIS 的远程主机上触发?

代码如下:

public abstract class BaseController : Controller
{
    public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
    {
            //This method fires on localhost but not on remote server (IIS)

            //call into base method
            base.OnActionExecuting(actionExecutingContext);

            //validate http context exists
            if (HttpContext == null)
                throw new Exception("The HttpContext should not be null.");

            //do other work...
    }
}

【问题讨论】:

  • 这很奇怪。你能在本地 IIS 中托管并调试它吗?
  • 这篇文章可能有用:andrewlock.net/…

标签: c# asp.net-core .net-core asp.net-core-webapi


【解决方案1】:

问题不是因为 actionfilters,因为 IIS express 中的请求正文问题,我遇到了同样的问题。

before reading request body need to EnableBuffering and after that need add Body.Position = 0

Solution 1:



context.Request.EnableBuffering();

    // Leave the body open so the next middleware can read it.
    using (var reader = new StreamReader(
        context.Request.Body,
        encoding: Encoding.UTF8,
        detectEncodingFromByteOrderMarks: false,
        bufferSize: bufferSize,
        leaveOpen: true))
    {
        var body = await reader.ReadToEndAsync();
        // Do some processing with body…

        // Reset the request body stream position so the next middleware can read it
        context.Request.Body.Position = 0;
    }

solution 2:

 private async Task<string> FormatRequest(HttpRequest request)
        {
            request.EnableBuffering();
            var requestBody = await ReadBodyFromPipeReader(request.BodyReader);
            request.Body.Position = 0;
            return requestBody;
        }

 private async Task<string> ReadBodyFromPipeReader(PipeReader reader)
        {
            StringBuilder stringBuilder = new StringBuilder();
            while (true)
            {
                // await some data being available
                ReadResult read = await reader.ReadAsync();
                ReadOnlySequence<byte> buffer = read.Buffer;
                // check whether we've reached the end
                // and processed everything
                if (buffer.IsEmpty && read.IsCompleted)
                    break; // exit loop

                // process what we received
                foreach (var segment in buffer)
                {
                    string asciString = Encoding.ASCII.GetString(
                        segment.Span);
                    stringBuilder.Append(asciString);
                }
                // tell the pipe that we used everything   
                reader.AdvanceTo(buffer.Start, buffer.End);
                if (read.IsCompleted)
                {
                    break;
                }

            }
            return Convert.ToString(stringBuilder);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2015-09-13
    • 2012-10-11
    • 2017-10-30
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多