ABP应用服务POST请求体部分

huangapple go评论61阅读模式
英文:

ABP Application Service Post With Request Body

问题

需要创建一个应用程序服务,其中包含一个POST方法。在这个方法中,逻辑需要访问请求正文以便处理它。您是否可以在ABP应用程序服务中实现这一点?我已经贴出了另一个服务的示例代码。

public class MedistatWebHookController : ControllerBase
{
    private readonly ILogger<MedistatWebHookController> _logger;

    public MedistatWebHookController(ILogger<MedistatWebHookController> logger)
    {
        _logger = logger;
        _logger.LogInformation(string.Format("Service Has Started {0}", DateTime.Now.ToString("dd MMM yyyy")));
    }

    [HttpPost]
    [Route("Events")]
    public async Task<IActionResult> ReceiveEvents()
    {
        try
        {
            _logger.LogInformation(string.Format("Start logging email post request from events {0}", DateTime.Now.ToString("dd MMM yyyy")));

            _logger.LogInformation(string.Format("Request Body {0}", Request.Body));

            var parser = new WebhookParser();
            var events = await parser.ParseEventsWebhookAsync(Request.Body).ConfigureAwait(false);

            foreach (var emailEvent in events)
            {
                var dump = ObjectDumper.Dump(emailEvent);
                string createText = dump;
                System.IO.File.WriteAllText("C:\\Medistat\\Logs\\EmailEvents.txt", createText);
            }

            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }
}

我不知道如何在应用程序服务方法中访问请求正文。我已经阅读了文档,但没有找到相关信息。

英文:

I need to create an application service which has a post methd, In this method the logic needs to access the request body in order to work with it, is it possible to do this inside an ABP application service, I have posted an example of another service.

public class MedistatWebHookController : ControllerBase
{

    private readonly ILogger&lt;MedistatWebHookController&gt; _logger;

    public MedistatWebHookController(ILogger&lt;MedistatWebHookController&gt; logger)
    {
        _logger = logger;
        _logger.LogInformation(string.Format(&quot;Service Has Started {0}&quot;, DateTime.Now.ToString(&quot;dd MMM yyyy&quot;)));
    }

    [HttpPost]
    [Route(&quot;Events&quot;)]
    public async Task&lt;IActionResult&gt; ReceiveEvents()
    {
        try
        {
            _logger.LogInformation(string.Format(&quot;Start logging email post request from events {0}&quot;, DateTime.Now.ToString(&quot;dd MMM yyyy&quot;)));

            _logger.LogInformation(string.Format(&quot;Request Body {0}&quot;, Request.Body));

            var parser = new WebhookParser();
            var events = await parser.ParseEventsWebhookAsync(Request.Body).ConfigureAwait(false);
            //Event event = new Event();

            foreach(var emailEvent in events)
            {
                var dump = ObjectDumper.Dump(emailEvent);
                // Create a file to write to
                string createText = dump;
                System.IO.File.WriteAllText(&quot;C:\\Medistat\\Logs\\EmailEvents.txt&quot;, createText);
            }


            return Ok(); 
        }
        catch (Exception ex) 
        { 
            return BadRequest(ex.Message);
        }
    }

I dont know how to access the request body inside the application service method. I have read through the documentation and I'm not seeing anything

答案1

得分: 1

以下是翻译好的部分:

"Since HttpContext is part of web layer, it's not true to use it in application layer. But if you still want, add reference to System.Web and use HttpContext.Current static property.

My advice would be to get the information you want from request on the controller side and pass it as a parameter to ApplicationService."

英文:

Since HttpContext is part of web layer, it's not true to use it in application layer. But if you still want, add reference to System.Web and use HttpContext.Current static property.

My advice would be to get the information you want from request on the controller side and pass it as a parameter to ApplicationService.

huangapple
  • 本文由 发表于 2023年4月11日 16:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定