Aşağıdaki gibi, eylemin niteliğini almak ve burada belirtilen veya ControllerContext.RequestContext
genişletilmiş olarak HttpContext.Current.Items
depolamak için ControllerActionInvoker.FindAction()
geçersiz olabilir:
public class MyControllerActionInvoker : ControllerActionInvoker
{
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
{
var action = base.FindAction(controllerContext, controllerDescriptor, actionName);
if (action != null)
{
var requestContext = ExtendedRequestContext.Bind(controllerContext);
var attr = action.GetCustomAttributes(typeof(MyAttribute), false).FirstOrDefault();
if (attr != null)
requestContext.CustomAttribute = (MyAttribute)attr;
}
return action;
}
}
public class ExtendedRequestContext : RequestContext
{
public MyAttribute CustomAttribute { get; set; }
public static ExtendedRequestContext Bind(ControllerContext controllerContext)
{
var requestContext = new ExtendedRequestContext
{
HttpContext = controllerContext.RequestContext.HttpContext,
RouteData = controllerContext.RequestContext.RouteData
};
controllerContext.RequestContext = requestContext;
return requestContext;
}
}
varsayılan eylem invoker sizin kontrolörün Oluşturucu veya bir ya değiştirilir özel kontrolörleri fabrika: Bu arada
public MyController() : base()
{
ActionInvoker = new MyControllerActionInvoker();
}
, Controller.TempData
zatenerişim sağlar ReflectedParameterDescriptor
tür bir öğe içerir, dolayısıyla yukarıdaki kod gereksiz olabilir. Ancak, bunun uygulamaya özgü olduğunu unutmayın, bu nedenle zamanla değişebilir.
Son olarak, bağlayıcı sınıfında o depodan niteliği olsun:
var requestContext = (ExtendedRequestContext)controllerContext.RequestContext;
if (requestContext.CustomAttribute != null)
{
// apply your logic here
}
bu 'bir eylem filtre veya düz CLR niteliğini MyAttribute' mı? Amacı nedir ve neden model bağlayıcı ile * bağlamanız gerekiyor? –
Evet, MyAttribute bir CLR Özniteliğidir. [ModelBinder (typod (MyModelBinder))] ile argümanları iletmenin bir yolu olmadığından yöntemi "bilgiler" ile imzalardım. – dknaack
@dknaack, bir yolu var: özel bir model binder sağlayıcısı kullanabilirsiniz. –