- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 Action 过滤器来根据传入的 id 检查 id 是否正确。
过滤器的构造方法接受一个服务类型参数。
在 ActionMethod 运行之前,我需要服务来检查此 ID。但是 GetService()
方法返回空值。
Action 过滤器
public class ContainsFilterAttribute : ActionFilterAttribute
{
private Type _service;
private Type _entity;
private IdSections _idSections;
public ContainsFilterAttribute(Type service, Type entity, IdSections idSections)
{
if (!typeof(ICommonService).IsAssignableFrom(service) || !typeof(IEntity).IsAssignableFrom(entity))
{
throw new System.Exception("Service or entity undefined.");
}
_service = service;
_entity = entity;
_idSections = idSections;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
//returns null service
var service = (ICommonService)context.HttpContext.RequestServices.GetService(_service);
var entity = (IEntity)Activator.CreateInstance(_entity);
if (IdSections.FromRoute == _idSections)
{
entity.Id = (int)context.ActionArguments.FirstOrDefault(pair => pair.Key.ToUpper().Contains("ID")).Value;
}
var res = service.Contains(entity);
}
}
操作方法
[HttpGet]
[Route("{userId}/user-channels")]
[ContainsFilter(typeof(UserManager), typeof(User), IdSections.FromRoute)]
public IActionResult GetUserChannels(int userId)
{
var result = _channelService.GetUserChannels(userId);
if (result.IsSuccessful)
{
var mapResult = _mapper.Map<List<ChannelForListDto>>(result.Data);
return Ok(mapResult);
}
return this.ServerError(result.Message);
}
注塑
services.AddScoped<IUserService, UserManager>();
最佳答案
IUserService
注册为服务
services.AddScoped<IUserService, UserManager>();
但您正在尝试解决实现
UserManager
在显示的属性中
[ContainsFilter(typeof(UserManager), typeof(User), IdSections.FromRoute)]
称呼
[ContainsFilter(typeof(IUserManager), typeof(User), IdSections.FromRoute)]
反而
关于c# - ActionFilterAttribute 中的 GetService 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66427642/
也许我误解了 ActionFilterAttribute 的要点,但我现在拥有的是以下内容: public class MyCustomAttribute : ActionFilterAttribut
我有这个自定义操作过滤器: public class PermissionChecker: ActionFilterAttribute { private int _permissionId
我做了一个自定义访问检查 ActionFilterAttribute,如果他们没有足够的访问权限,我想将其重定向到错误页面。这是我到目前为止所拥有的。 RouteValueDictionary rou
我遇到了 ActionFilterAttriute 无法正确重定向的问题。我不熟悉完整的代码库,但我已经看到了足够多的代码,不明白发生了什么。 为了简化代码,我删除了不相关的部分: public cl
我的 ActionFilter 的调用顺序有问题。 我创建了一个过滤器来设置布局 MasterName: [AttributeUsage(AttributeTargets.Method | Attri
例子 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple
我创建了一个 Action 过滤器,用于测量 Web API v2 中每个 Action 的运行时间。 public class RunningTimeAttribute : ActionFilter
我正在尝试对自定义 ActionFilterAttribute 进行单元测试。例如: public class ActionAuthorizationAttribute: ActionFilterAt
public class CheckMetadataAttribute : ActionFilterAttribute { public override void OnActionExecu
我正在实现自定义 ActionFilterAttribute,目前尚不清楚我是否需要覆盖 OnActionExecutingAsync() 方法。这是一个使用 ASP.NET WebAPI v5.
我想在 MVC2 上定义以下属性: public class BackAttribute : ActionFilterAttribute { public BackAttribute(objec
我的 Controller 有一个属性 [ABC] MyController 属性会检查一些内容,但我只想每页检查一次。 public class ABCAttribute : ActionFilt
我正在尝试理解这个 ActionFilterAttribute。我尝试稍微尝试一下代码以更好地理解它是如何工作的,但我完全迷路了。 这是有效的 ActionFilterAttribute。它应该检查空
我为我的 web api 创建了一个 ActionFilterAttribute 来授权人们。通过 RequestUri 获取 accessToken 是可以的,但是我想以表单数据的形式发送它。在 A
书面形式this answer , 有人问我是否有关于 ActionFilterAttribute 的行为的保证.我无法自信地回答。 特别是 OnActionExecuted、OnActionExec
在我的 WebAPI 项目中,我正在使用 FluentValidation。我通过添加在全局范围内启用它FluentValidationModelValidatorProvider.Configure
开始编写一个简单的过滤器来在每个 Action 加载时从请求中提取一些东西,从其他 stackoverflows 复制一些代码,如下所示: public class TestKeyAttribute
我想在 ActionFilterAttribute 中有一个字段,它只与它所在的操作相关,例如 public class TimedAction : ActionFilterAttribute
再会。 我正在尝试通过在我的自定义 ActionFilterAttribute 类中注入(inject) LoggerFactory 来使用日志记录,但是在其中一个 Controller 方法中使用
在注释的 Controller 方法上 ActionFilterAttribute哪个先发生,是 OnActionExecuting 还是 OnResulting 方法? 事情发生的顺序是什么?我已经
我是一名优秀的程序员,十分优秀!