asp.net中action过滤器如何添加参数?我有以下过滤器属性,我可以将字符串数组传递给[MyAttribute("string1","string2")]之类的属性。公共类MyAttribute:TypeFilterAttribute{privatereadonlystring[]_ids;publicMyAttribute(paramsstring[]ids):base(typeof(MyAttributeImpl)){_ids=ids;ILoggerFactoryloggerFactory){_logger=loggerFactory.CreateLogger();}publicvoidOnActionExecuting(ActionExecutingContextcontext){//我如何在此处访问ID变量???}publicvoidOnActionExecuted(ActionExecutedContextcontext){}}}Array_ids传递给动作过滤器的实现?我错过了一些非常明显的东西!?TypedFilterAttribute有一个Argument属性(类型为object[]),您可以在其中将参数传递给已实现的构造函数。所以,应用到你的例子中,你可以使用这样的代码:以上是C#学习教程:如何在asp.net中的actionfilter中添加参数?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——publicclassMyAttribute:TypeFilterAttribute{publicMyAttribute(paramsstring[]ids):base(typeof(MyAttributeImpl)){Arguments=newobject[]{ids};}privateclassMyAttributeImpl:IActionFilter{privatereadonlystring[]_ids;私人只读ILogger_logger;publicMyAttributeImpl(ILoggerFactoryloggerFactory,string[]ids){CregerFlogacgerLogger;日志_();}publicvoidOnActionExecuting(ActionExecutingContextcontext){//NOWYOUCANACCESS_idsforeach(varidin_ids){}}publicvoidOnActionExecuted(ActionExecutedContextcontext){}}}本文来源于网络,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
