【发布时间】:2018-07-03 22:29:48
【问题描述】:
在我的项目中,我使用日志中间件记录每个请求。如何获取与请求匹配的路由以进行日志记录?
我在请求中有完整路径,例如/v1/User/123
但我想记录这个:/v1/User/{id}
这是我目前所拥有的:
public async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var path = context.ActionDescriptor.AttributeRouteInfo.Template;
await next();
}
这是在我的基本控制器中,我如何将它发送到日志中间件?
这是我如何将它发送到日志中间件的:
基础控制器:
public async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var actionPath = context.ActionDescriptor.AttributeRouteInfo.Template;
HttpContext.Items.Add("ActionInfo", actionPath);
await next();
}
日志中间件:
var actionInfo = context.Items["ActionInfo"];
if (actionInfo != null)
{
actionMatched = actionInfo.ToString();
}
有没有更好的方法来做到这一点? 此外,这仅在您使用属性路由时才有效。如果您在启动时注册路由,这将如何工作?
【问题讨论】:
标签: asp.net-mvc asp.net-core-mvc asp.net-core-2.1