मैं जानता हूँ कि मेरा उत्तर एक छोटे से देर से (लगभग चार वर्ष) खेल के लिए है, लेकिन मैं इस सवाल में आए और मैं जिस समाधान को तैयार करता हूं उसे साझा करना चाहता था जो मुझे मूल प्रश्न करना चाहता था, अगर भविष्य में किसी और की मदद करता है।
समाधान में AttributeUsage
नामक एक छोटे मणि शामिल है, जो हमें नियंत्रक (और यहां तक कि किसी भी बेस नियंत्रक!) पर एक विशेषता निर्दिष्ट करने की अनुमति देता है और फिर आवश्यकतानुसार व्यक्तिगत क्रियाओं या उप-नियंत्रकों पर ओवरराइड (अनदेखा/हटा) देता है। वे "कैस्केड" करेंगे जहां वास्तव में सबसे अधिक ग्रैन्युलर विशेषता वास्तव में आग लगती है: यानी, वे कम-विशिष्ट (बेस नियंत्रक) से अधिक विशिष्ट (व्युत्पन्न नियंत्रक) तक, सबसे विशिष्ट (क्रिया विधियों) तक जाते हैं।
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, Inherited=true, AllowMultiple=false)]
public class MyCustomFilterAttribute : ActionFilterAttribute
{
private MyCustomFilterMode _Mode = MyCustomFilterMode.Respect; // this is the default, so don't always have to specify
public MyCustomFilterAttribute()
{
}
public MyCustomFilterAttribute(MyCustomFilterMode mode)
{
_Mode = mode;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (_Mode == MyCustomFilterMode.Ignore)
{
return;
}
// Otherwise, respect the attribute and work your magic here!
//
//
//
}
}
public enum MyCustomFilterMode
{
Ignore = 0,
Respect = 1
}
(मैं आप विशेषताओं की तरह सुना, तो मैं विशेषता पर कुछ विशेषताओं डाल यह वास्तव में बहुत ऊपर यहाँ जादू काम करता है क्या करना है::
यहाँ है कैसे! अनुमति दे उन्हें/झरना वारिस के, लेकिन उनमें से केवल एक के लिए अनुमति देता निष्पादित करने के लिए)
यहां बताया गया है कि अब यह प्रयोग किया जाता है:।
[MyCustomFilter]
public class MyBaseController : Controller
{
// I am the application's base controller with the filter,
// so any derived controllers will ALSO get the filter (unless they override/Ignore)
}
public class HomeController : MyBaseController
{
// Since I derive from MyBaseController,
// all of my action methods will also get the filter,
// unless they specify otherwise!
public ActionResult FilteredAction1...
public ActionResult FilteredAction2...
[MyCustomFilter(Ignore)]
public ActionResult MyIgnoredAction... // I am ignoring the filter!
}
[MyCustomFilter(Ignore)]
public class SomeSpecialCaseController : MyBaseController
{
// Even though I also derive from MyBaseController, I can choose
// to "opt out" and indicate for everything to be ignored
public ActionResult IgnoredAction1...
public ActionResult IgnoredAction2...
// Whoops! I guess I do need the filter on just one little method here:
[MyCustomFilter]
public ActionResult FilteredAction1...
}
मुझे आशा है कि इस संकलित, मैं कुछ इसी तरह के कोड से यह yanked और एक छोटे से रों किया इस पर earch-and-replace ताकि यह सही नहीं हो सकता है।
धन्यवाद डेविड! वास्तव में मेरे मन में क्या था! –