英文:
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<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);
//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("C:\\Medistat\\Logs\\EmailEvents.txt", 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论