IActionContextAccessor为空 [英] IActionContextAccessor Is Null

查看:277
本文介绍了IActionContextAccessor为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为.NET Core应用程序设置自定义中间件,以记录异常错误,并在Startup.cs中进行以下操作以注册我的上下文:

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
    }
}

我试图从中获取ActionContext.RouteData值的'contextAccessor'似乎为Null.

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

我做错了什么?谢谢.

推荐答案

IActionContextAccessor.ActionContext在mvc中间件范围之外为null.

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

如果您在异常处理程序中仅关心的是MVC操作,则可以创建一个操作过滤器而不是中间件来捕获和记录异常.

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);
    }
}

这篇关于IActionContextAccessor为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆