简体   繁体   English

将方法返回的值传递给操作过滤器

[英]pass value returned from a method to action filter

I have _serviceOne injected into my controller that has a method which returns an int value.我将_serviceOne注入到我的控制器中,该控制器有一个返回int值的方法。 I'm trying to pass this value into my custom action filter.我正在尝试将此值传递到我的自定义操作过滤器中。

This isn't working and I am getting the error: An object reference is required for the non-static field, method, or property 'NameController._serviceOne' where I try to set the Number = _serviceOne.GetIntNumber .这不起作用,我收到错误消息:我尝试设置Number = _serviceOne.GetIntNumber An object reference is required for the non-static field, method, or property 'NameController._serviceOne'

I am aware that I can access a value if it is inside the controller (eg: controller parameter, ViewBag, ViewData, a variable in the controller), but I want to pass the value to the CustomActionFilter filter's Number property.我知道我可以访问控制器内部的值(例如:控制器参数、ViewBag、ViewData、控制器中的变量),但我想将该值传递给CustomActionFilter过滤器的Number属性。

The filter and service method work the way I want it to, but it won't let me pass the value from _serviceOne.GetIntNumber into the filter.过滤器和服务方法按我希望的方式工作,但它不会让我将值从_serviceOne.GetIntNumber传递到过滤器中。 Why is this not working, and how could I make it work?为什么这不起作用,我怎样才能使它起作用?

NameController.cs:名称控制器.cs:

public class NameController : Controller
{
    private readonly ServiceOne _serviceOne;

    public NameController(ServiceOne serviceOne)
    {
        _serviceOne = serviceOne;
    }

    [CustomActionFilter(Name = "CorrectName", Number = _serviceOne.GetIntNumber)] //does not work
    [HttpGet]
    public IActionResult Index()
    {
        return View();
    }
}

CustomActionFilter.cs: CustomActionFilter.cs:

public class CustomActionFilter : ActionFilterAttribute
{
    public string Name { get; set; }
    public int Number { get; set; }

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        if (Name == "CorrectName" && Number == 1) {
            RouteValueDictionary routeDictionary = new RouteValueDictionary { { "action", "SomeAction" }, { "controller", "NameController" } };
            context.Result = new RedirectToRouteResult(routeDictionary);
        }

        base.OnActionExecuted(context);
    }
}

Attributes are created at compile time so it's not possible to pass them run-time values in the constructor.属性是在编译时创建的,因此不可能在构造函数中传递它们的运行时值。

Instead, you can access NameController 's instance of the service, like this:相反,您可以访问NameController的服务实例,如下所示:

public class NameController : Controller
{
    private readonly ServiceOne _serviceOne;

    public ServiceOne ServiceOne => _serviceOne;

    public NameController(ServiceOne serviceOne)
    {
        _serviceOne = serviceOne;
    }
}

public class CustomActionFilter : ActionFilterAttribute
{
    public string Name { get; set; }
    public int Number { get; set; }

    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var controller = context.Controller as NameController;
        var service = controller.ServiceOne;
        //Use the service here
    }
}

See also Access a controller's property from an action filter另请参阅从操作过滤器访问控制器的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM