2015-08-07 20 views
34

Dựa trên this article Tôi đang cố tạo một triển khai IActionFilter cho ASP.NET Core có thể xử lý các thuộc tính được đánh dấu trên bộ điều khiển và hành động của bộ điều khiển. Mặc dù đọc các thuộc tính của bộ điều khiển là dễ dàng, tôi không thể tìm cách đọc các thuộc tính được định nghĩa trên phương thức hành động.Làm thế nào để đọc thuộc tính của phương thức hành động trong ASP.NET Core MVC?

Dưới đây là đoạn code tôi có ngay bây giờ:

public sealed class ActionFilterDispatcher : IActionFilter 
{ 
    private readonly Func<Type, IEnumerable> container; 

    public ActionFilterDispatcher(Func<Type, IEnumerable> container) 
    { 
     this.container = container; 
    } 

    public void OnActionExecuting(ActionExecutingContext context) 
    { 
     var attributes = context.Controller.GetType().GetCustomAttributes(true); 

     attributes = attributes.Append(/* how to read attributes from action method? */); 

     foreach (var attribute in attributes) 
     { 
      Type filterType = typeof(IActionFilter<>).MakeGenericType(attribute.GetType()); 
      IEnumerable filters = this.container.Invoke(filterType); 

      foreach (dynamic actionFilter in filters) 
      { 
       actionFilter.OnActionExecuting((dynamic)attribute, context); 
      } 
     } 
    } 

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

Câu hỏi của tôi là: làm thế nào để tôi đọc thuộc tính phương pháp hành động trong ASP.NET MVC cốt lõi?

+0

Bạn sẽ nhận được 'MemberInfo' cho bất kỳ phương pháp nào bạn quan tâm sử dụng API phản chiếu, sau đó sử dụng' GetCustomAttributes' trên đó. Hy vọng rằng tôi không hiểu sai câu hỏi –

+0

@Asad: chỉ cho tôi. – Steven

Trả lời

36

Bạn có thể truy cập vào MethodInfo của hành động thông qua các lớp ControllerActionDescriptor:

public void OnActionExecuting(ActionExecutingContext context) 
{ 
    var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor; 
    if (controllerActionDescriptor != null) 
    { 
     var actionAttributes = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true); 
    } 
} 

Các MVC 5 ActionDescriptor lớp được sử dụng để thực hiện các giao diện ICustomAttributeProvider đó đã truy cập vào các thuộc tính. Vì một số lý do, điều này đã bị loại bỏ trong lớp ASP.NET Core MVC ActionDescriptor.

+0

Làm thế nào tôi có thể làm điều đó trong asp.net mvc 6 – MichaelMao

+0

Đây là về MVC 6 (hoặc Core 1.0). –

+0

Sai lầm của tôi tôi không nhìn rõ ràng. Bạn có thể kiểm tra câu trả lời của tôi là đúng hay không? Tôi chỉ tìm cách để có được thuộc tính và sẽ đăng nó. – MichaelMao

3

Tôi đã tạo phương thức tiện ích bắt chước hình ảnh gốc GetCustomAttributes có trụ sở tại giải pháp của Henk Mollema.

public static IEnumerable<T> GetCustomAttributes<T>(this Microsoft.AspNet.Mvc.Abstractions.ActionDescriptor actionDescriptor) where T : Attribute 
    { 
     var controllerActionDescriptor = actionDescriptor as ControllerActionDescriptor; 
     if (controllerActionDescriptor != null) 
     { 
      return controllerActionDescriptor.MethodInfo.GetCustomAttributes<T>(); 
     } 

     return Enumerable.Empty<T>(); 
    } 

Hy vọng điều đó sẽ hữu ích.

3

Thuộc tính tùy chỉnh của tôi được kế thừa từ ActionFilterAttribute. Tôi đặt nó trên bộ điều khiển của tôi nhưng có một hành động không cần nó. Tôi muốn sử dụng thuộc tính AllowAnonymous để bỏ qua điều đó nhưng nó không hoạt động. Vì vậy, tôi thêm đoạn mã này vào thuộc tính tùy chỉnh của tôi để tìm số AllowAnonymous và bỏ qua nó. Bạn có thể nhận được khác trong vòng lặp for.

public class PermissionAttribute : ActionFilterAttribute 
    { 
     public override void OnActionExecuting(ActionExecutingContext context) 
     { 
      foreach (var filterDescriptors in context.ActionDescriptor.FilterDescriptors) 
      { 
       if (filterDescriptors.Filter.GetType() == typeof(AllowAnonymousFilter)) 
       { 
        return; 
       } 
      } 
     } 
    } 
+0

Thuộc tính 'FilterDescriptors' chỉ hoạt động nếu thuộc tính tùy chỉnh của bạn thực sự là [Bộ lọc MVC] (https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters). Nếu bạn có một [thuộc tính thụ động] tùy chỉnh (http://blog.ploeh.dk/2014/06/13/passive-attributes/) được áp dụng cho hành động của bạn, thì rõ ràng giải pháp của bạn sẽ không hoạt động. – QuantumHive

Các vấn đề liên quan