相关疑难解决方法(0)

如何查找所有控制器和操作

如何在dotnet核心中查找其属性的所有控制器和操作?在.NET Framework中,我使用了以下代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

但它不适用于dotnet核心.

c# asp.net-core-mvc asp.net-core

9
推荐指数
1
解决办法
1619
查看次数

如何在ASP.Net Core MVC应用中获取操作和控制器名称?

如何在Application中获取ActionController命名?ASP.Net MVC Core RC1Startup.cs

我想创建一个中间件和登录下面的代码后的信息(我想记录详细的回应我的数据库,所以我需要行动和控制器的信息。)configure的方法startup.cs-

 app.UseMvc(routes =>
{
     routes.MapRoute(
            name: "default",
            template: "{controller=User}/{action=Index}/{id?}");
 });

//Want to get Action and controller names here..
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core asp.net-core-1.0

3
推荐指数
2
解决办法
7478
查看次数