2011-08-20 13 views
5

मैं एक एक्शन और निम्नलिखित के रूप में विशेषता हो रहा है में विशेषता पढ़ना, मैं OnActionExecuting से अधिक ग्रस्त और उस विधि में विशेषता पढ़ना चाहते हैOnAction asp.net mvc3 में निष्पादित

[MyAttribute(integer)] 
public ActionResult MyAction() 
{ 
} 


protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    //here i want to read integer passed to action using Attribute 
} 

उत्तर

10

यह प्रयास करें:

नियंत्रक

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    foreach (var filter in filterContext.ActionDescriptor.GetCustomAttributes(typeof (MyAttribute), false).Cast<MyAttribute>()) 
    { 
    var desiredValue = filter.Parameter; 
    } 

    base.OnActionExecuting(filterContext); 
} 

फ़िल्टर

public class MyAttribute : FilterAttribute, IActionFilter 
{ 
    private readonly int _parameter; 

    public MyAttribute(int parameter) 
    { 
    _parameter = parameter; 
    } 

    public int Parameter { get { return _parameter; } } 

    public void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
    //throw new NotImplementedException(); 
    } 

    public void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
    //throw new NotImplementedException(); 
    } 
} 
+0

एक्शन फ़िल्टर की पूरी व्याख्या के लिए, इस आलेख को देखें: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/समझ-एक्शन फिल्टर-सीएस –

संबंधित मुद्दे