c# - 从 ASP.NET Core 中的 ExceptionFilterAttribute 访问 Controller

标签 c# asp.net-core

使用 ASP.Net Core 2,您如何访问应用了 ExceptionFilterAttribute 的 Controller 实例?

现在在 Core 中是否有更好的方法来实现共享的“基本” Controller 属性和方法等?比如放到Startup这样更高的层级?

在 Core 之前,在 MVC 4 中,我会做这样的事情:

/// <summary>
/// Base controller, shared by all WebAPI controllers for tracking and shared properties.
/// </summary>
[ApiTracking]
[ApiException]
public class BaseApiController : ApiController
{
    private Common.Models.Tracking _Tracking = null;
    public Common.Models.Tracking Tracking
    {
        get
        {
           if(_Tracking == null)
               _Tracking = new Common.Models.Tracking();
           return _Tracking;
        }
    }
    //... other shared properties...
}

/// <summary>
/// Error handler for webapi calls. Adds tracking to base controller.
/// </summary>
public class ApiExceptionAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext cntxt)
    {
        BaseApiController ctrl = cntxt.ActionContext.ControllerContext.Controller as BaseApiController;
        if (ctrl != null)
            ctrl.Tracking.Exception(cntxt.Exception, true);

        base.OnException(actionExecutedContext);
    }
}

/// <summary>
/// Intercepts all requests to inject tracking detail and call tracking.save.
/// </summary>
public class ApiTrackingAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext cntxt)
    {
        //...add info to tracking
    }
    public override void OnActionExecuted(HttpActionExecutedContext cntxt)
    {
        BaseApiController ctrl = cntxt.ActionContext.ControllerContext.Controller as BaseApiController;
        if (ctrl != null)
            ctrl.Tracking.Save();
    }
}

最佳答案

HttpContext在 ASP.Net Core 中包含 Items IDictionary<object, object>的属性(property)在请求范围内共享数据的类型。这正是您处理案件所需要的。这是一个示例实现:

public class ApiExceptionAttribute : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        var items = context.HttpContext.Items;
        if (items.ContainsKey("Tracking"))
        {
            Tracking tracking = (Tracking)items["Tracking"];
            tracking.Exception(context.Exception, true);
        }

        base.OnException(context);
    }
}

public class ApiTrackingAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var tracking = new Tracking();
        //...add info to tracking

        context.HttpContext.Items.Add("Tracking", tracking);
    }

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var items = context.HttpContext.Items;
        if (items.ContainsKey("Tracking"))
        {
            Tracking tracking = (Tracking) items["Tracking"];
            tracking.Save();
        }
    }
}

关于c# - 从 ASP.NET Core 中的 ExceptionFilterAttribute 访问 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898410/

相关文章:

c# - 在 C# 中连接到 SQL 数据库

c# - 如何在窗口中捕获数据

asp.net-core - StackExchange.Redis.StrongName 被引用但不包含在包中

c# - 如何使用 ADO.NET 获取表中列的 SqlDbType?

c# - 如何防止在公式绑定(bind)单元格中显示绿色警告三角形(C# Excel Interop)?

c# - 我收到错误 "There is no ViewData item of type ' IEnumerable<SelectListItem>'

c# - 在自定义中间件中测试 response.WriteAsync()

c# - ASP.NET 5 依赖错误

c# - 如何在Asp.net Core中获取用户浏览器名称( user-agent )?

linux - .NET Core 的 MongoDB C# 驱动程序中不受支持的位置运算符的解决方法