• <bdo id='Rdh4Y'></bdo><ul id='Rdh4Y'></ul>
  1. <i id='Rdh4Y'><tr id='Rdh4Y'><dt id='Rdh4Y'><q id='Rdh4Y'><span id='Rdh4Y'><b id='Rdh4Y'><form id='Rdh4Y'><ins id='Rdh4Y'></ins><ul id='Rdh4Y'></ul><sub id='Rdh4Y'></sub></form><legend id='Rdh4Y'></legend><bdo id='Rdh4Y'><pre id='Rdh4Y'><center id='Rdh4Y'></center></pre></bdo></b><th id='Rdh4Y'></th></span></q></dt></tr></i><div id='Rdh4Y'><tfoot id='Rdh4Y'></tfoot><dl id='Rdh4Y'><fieldset id='Rdh4Y'></fieldset></dl></div>
      <legend id='Rdh4Y'><style id='Rdh4Y'><dir id='Rdh4Y'><q id='Rdh4Y'></q></dir></style></legend>
    1. <tfoot id='Rdh4Y'></tfoot>

      <small id='Rdh4Y'></small><noframes id='Rdh4Y'>

    2. 如何找到所有控制器和动作

      时间:2023-07-10
      <legend id='5BsYg'><style id='5BsYg'><dir id='5BsYg'><q id='5BsYg'></q></dir></style></legend>
        <bdo id='5BsYg'></bdo><ul id='5BsYg'></ul>
          <tbody id='5BsYg'></tbody>

        • <i id='5BsYg'><tr id='5BsYg'><dt id='5BsYg'><q id='5BsYg'><span id='5BsYg'><b id='5BsYg'><form id='5BsYg'><ins id='5BsYg'></ins><ul id='5BsYg'></ul><sub id='5BsYg'></sub></form><legend id='5BsYg'></legend><bdo id='5BsYg'><pre id='5BsYg'><center id='5BsYg'></center></pre></bdo></b><th id='5BsYg'></th></span></q></dt></tr></i><div id='5BsYg'><tfoot id='5BsYg'></tfoot><dl id='5BsYg'><fieldset id='5BsYg'></fieldset></dl></div>

          <small id='5BsYg'></small><noframes id='5BsYg'>

        • <tfoot id='5BsYg'></tfoot>

                本文介绍了如何找到所有控制器和动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何在 dotnet core 中找到所有具有其属性的控制器和动作?在 .NET Framework 中,我使用了以下代码:

                How to find all controllers and actions with its attribute in dotnet core? In .NET Framework I used this code:

                public static List<string> GetControllerNames()
                {
                    List<string> controllerNames = new List<string>();
                    GetSubClasses<Controller>().ForEach(type => controllerNames.Add(type.Name.Replace("Controller", "")));
                    return controllerNames;
                }
                public static List<string> ActionNames(string controllerName)
                {
                    var types =
                        from a in AppDomain.CurrentDomain.GetAssemblies()
                        from t in a.GetTypes()
                        where typeof(IController).IsAssignableFrom(t) &&
                            string.Equals(controllerName + "Controller", t.Name, StringComparison.OrdinalIgnoreCase)
                    select t;
                
                    var controllerType = types.FirstOrDefault();
                
                    if (controllerType == null)
                    {
                        return Enumerable.Empty<string>().ToList();
                    }
                    return new ReflectedControllerDescriptor(controllerType)
                       .GetCanonicalActions().Select(x => x.ActionName).ToList();
                }
                

                但它在 dotnet core 中不起作用.

                but its not working in dotnet core.

                推荐答案

                如何将 IActionDescriptorCollectionProvider 注入到需要知道这些事情的组件中?它位于 Microsoft.AspNetCore.Mvc.Infrastructure 命名空间中.

                How about injecting IActionDescriptorCollectionProvider to your component that needs to know these things? It's in the Microsoft.AspNetCore.Mvc.Infrastructure namespace.

                此组件为您提供应用程序中可用的每一个操作.以下是它提供的数据示例:

                This component gives you every single action available in the app. Here is an example of the data it provides:

                作为奖励,您还可以评估所有过滤器、参数等.

                As a bonus, you can also evaluate all of the filters, parameters etc.

                附带说明一下,我想您可以使用反射来查找从 ControllerBase 继承的类型.但是你知道你可以拥有不继承它的控制器吗?并且您可以编写覆盖这些规则的约定?出于这个原因,注入上述组件使其变得容易得多.您无需担心它会损坏.

                As a side note, I suppose you could use reflection to find the types that inherit from ControllerBase. But did you know you can have controllers that don't inherit from it? And that you can write conventions that override those rules? For this reason, injecting the above component makes it a lot easier. You don't need to worry about it breaking.

                这篇关于如何找到所有控制器和动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:ASP.Net Core 2 ServiceProviderOptions.ValidateScopes 属性 下一篇:ASP.NET Core 发布数组对象 JSON

                相关文章

                    <bdo id='bn2Bt'></bdo><ul id='bn2Bt'></ul>
                  <i id='bn2Bt'><tr id='bn2Bt'><dt id='bn2Bt'><q id='bn2Bt'><span id='bn2Bt'><b id='bn2Bt'><form id='bn2Bt'><ins id='bn2Bt'></ins><ul id='bn2Bt'></ul><sub id='bn2Bt'></sub></form><legend id='bn2Bt'></legend><bdo id='bn2Bt'><pre id='bn2Bt'><center id='bn2Bt'></center></pre></bdo></b><th id='bn2Bt'></th></span></q></dt></tr></i><div id='bn2Bt'><tfoot id='bn2Bt'></tfoot><dl id='bn2Bt'><fieldset id='bn2Bt'></fieldset></dl></div>

                    <tfoot id='bn2Bt'></tfoot>
                  1. <legend id='bn2Bt'><style id='bn2Bt'><dir id='bn2Bt'><q id='bn2Bt'></q></dir></style></legend>
                  2. <small id='bn2Bt'></small><noframes id='bn2Bt'>