Stackoverflow:IActionContextAccessor Is Null

问题:

I am setting up a custom Middleware for my .NET Core Application to log exception errors and have the following in the Startup.cs to register my contexts:

   services.AddHttpContextAccessor();
   services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

And in my Configure I have added the following to use a custom middleware:

app.UseMiddleware<CustomMiddleware>();

My custom Middleware class is the following:

 public class CustomMiddleware
 {
    private readonly RequestDelegate next;

    public CustomMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            this.BeginInvoke(context);
            await this.next.Invoke(context);
            this.EndInvoke(context);
        }
        catch(Exception ex)
        {
            //Capture the exception.
            string hostName = Environment.MachineName;
            string url = StringFunctions.getCurrentUrl(context);
            string userName = string.Empty;
            string controllerName = string.Empty;
            string actionName = string.Empty;

            //THIS IS NULL BELOW.
            IActionContextAccessor contextAccessor = context.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
            RouteData routeData = contextAccessor.ActionContext.RouteData;

            if (context.User != null)
            {
                userName = context.User.Identity.Name;
            }

            if (routeData != null && routeData.Values != null)
            {
                controllerName = routeData.Values["controller"].ToString();
                actionName = routeData.Values["action"].ToString();
            }

            EventLogging.LogApplicationError(hostName, controllerName, actionName, url, userName, ex);
        }
    }

    private void BeginInvoke(HttpContext context)
    {
        //custom work here
    }

    private void EndInvoke(HttpContext context)
    {
        // Do custom work after controller execution
    }
}

It seems that the ‘contextAccessor’ that I am trying to get the ActionContext.RouteData values from is Null.

What am I doing wrong? Thank you.

答案:

IActionContextAccessor.ActionContext is null outside the scope of mvc middleware.

If all you care about in your exception handler is MVC actions, you create an action filter instead of middleware to capture and log exceptions.

public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        //Capture the exception.
        string hostName = Environment.MachineName;
        string url = StringFunctions.getCurrentUrl(context.HttpContext);
        string userName = string.Empty;
        string controllerName = string.Empty;
        string actionName = string.Empty;

        if (context.HttpContext.User != null)
        {
            userName = context.HttpContext.User.Identity.Name;
        }

        controllerName = context.RouteData.Values["controller"].ToString();
        actionName = context.RouteData.Values["action"].ToString();

        EventLogging.LogApplicationError(hostName, controllerName, actionName, url, userName, ex);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值