Get list of custom attributes for current action/controller in ASP.NET MVC

Вы можете сделать это, отправив следующий запрос патча:

PATCH https://graph.microsoft.com/v1.0/groups/<groupid>/onenote/pages/<pageid> HTTP/1.1
Authorization: <bearer token>
User-Agent: Java-Client
Accept-Encoding: gzip,deflate
Accept: application/json, text/json
Content-Type: application/json
Host: graph.microsoft.com
Connection: keep-alive
Content-Length: 14

{"level": "1"}
14
задан Ruben Bartelink 22 January 2016 в 22:27
поделиться

2 ответа

Кажется, это работает ... есть ли лучший / более подходящий способ в ASP.NET MVC 1?

if (filterContext.Controller.GetType().GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0)
    return;
string action = (string)filterContext.RouteData.Values["action"];
if (!string.IsNullOrEmpty(action) && filterContext.Controller.GetType().GetMethod(action).GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0)
    return;
8
ответ дан 1 December 2019 в 06:38
поделиться

это работало на меня в.NET Core 2.2:

var controllerActionDescriptor = actionContext. ActionDescriptor как ControllerActionDescriptor;

        if (controllerActionDescriptor != null)
        {
            // Check if the attribute exists on the action method
            if (controllerActionDescriptor.MethodInfo?.GetCustomAttributes(inherit: true)?.Any(a => a.GetType().Equals(typeof(CustomAttribute))) ?? false)
                return true;

            // Check if the attribute exists on the controller
            if (controllerActionDescriptor.ControllerTypeInfo?.GetCustomAttributes(typeof(CustomAttribute), true)?.Any() ?? false)
                return true;
        }
0
ответ дан 1 December 2019 в 06:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: