将服务注入行动过滤器 [英] Inject service into Action Filter
问题描述
我试图在我的动作过滤器中注入一个服务,但是我没有在构造函数中注入所需的服务。这是我有的:
public class EnsureUserLoggedIn:ActionFilterAttribute
{
private readonly ISessionService _sessionService;
public EnsureUserLoggedIn()
{
//由于编译错误而使用
/我无法删除默认的ctor
// /属性在我的控制器
}
public EnsureUserLoggedIn(ISessionService sessionService)
{
_sessionService = sessionService;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
//问题:_sessionService为null这里
if(_sessionService.LoggedInUser == null )
{
context.HttpContext.Response.StatusCode =(int)HttpStatusCode.Unauthorized;
context.Result = new JsonResult(Unauthorized);
}
}
}
我正在装饰我的控制器如下所示:
[Route(api / issues),EnsureUserLoggedIn]
public class IssueController:Controller
{
}
Startup.cs
services.AddScoped< ISessionService,SessionService>();
使用这些文章作为参考:
ASP.NET 5和MVC 6中的动作过滤器,服务过滤器和类型过滤器
过滤器作为ServiceFilter
由于过滤器将用作 ServiceType
,因此需要注册框架IoC。如果直接使用操作过滤器,则不需要。
Startup.cs
public void ConfigureServices(IServiceCollection services){
services.AddMvc();
services.AddScoped< ISessionService,SessionService>();
services.AddScoped< EnsureUserLoggedIn>();
...
}
自定义过滤器被添加到MVC控制器方法和控制器类使用 ServiceFilter
属性,如下所示:
[ServiceFilter(typeof(EnsureUserLoggedIn))]
[Route(api / issues)]
public class IssueController:Controller {
// GET:api / issues
[HttpGet]
[ServiceFilter(typeof(EnsureUserLoggedIn))]
public IEnumerable< string> Get(){...}
}
还有其他例子
-
使用过滤器作为全局过滤器
-
使用过滤器使用基本控制器
-
使用具有订单的过滤器
看看,请试试看看是否解决了你的问题。
希望这有帮助。
I am trying to inject a service into my action filter but I am not getting the required service injected in the constructor. Here is what I have:
public class EnsureUserLoggedIn : ActionFilterAttribute
{
private readonly ISessionService _sessionService;
public EnsureUserLoggedIn()
{
// I was unable able to remove the default ctor
// because of compilation error while using the
// attribute in my controller
}
public EnsureUserLoggedIn(ISessionService sessionService)
{
_sessionService = sessionService;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
// Problem: _sessionService is null here
if (_sessionService.LoggedInUser == null)
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Result = new JsonResult("Unauthorized");
}
}
}
And I am decorating my controller like so:
[Route("api/issues"), EnsureUserLoggedIn]
public class IssueController : Controller
{
}
Startup.cs
services.AddScoped<ISessionService, SessionService>();
Using these articles as reference:
ASP.NET Core 1.0 Action Filters
Action filters, service filters and type filters in ASP.NET 5 and MVC 6
Using the filter as a ServiceFilter
Because the filter will be used as a ServiceType
, it needs to be registered with the framework IoC. If the action filters were used directly, this would not be required.
Startup.cs
public void ConfigureServices(IServiceCollection services) {
services.AddMvc();
services.AddScoped<ISessionService, SessionService>();
services.AddScoped<EnsureUserLoggedIn>();
...
}
Custom filters are added to the MVC controller method and the controller class using the ServiceFilter
attribute like so:
[ServiceFilter(typeof(EnsureUserLoggedIn))]
[Route("api/issues")]
public class IssueController : Controller {
// GET: api/issues
[HttpGet]
[ServiceFilter(typeof(EnsureUserLoggedIn))]
public IEnumerable<string> Get(){...}
}
There were other examples of
Using the filter as a global filter
Using the filter with base controllers
Using the filter with an order
Take a look, give them a try and see if that resolves your issue.
Hope this helps.
这篇关于将服务注入行动过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!