如何在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核心.
如何在Application中获取Action和Controller命名?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)