URL 授权的 Mvc Areas-Controller-Action 列表
Mvc Areas-Controller-Action Listing for URL Authorize
它可以是不同的方法。结果,我想从应用程序中获取所有 url。
我计划在我的应用程序中授权。而且,我需要这些(区域名称、控制器名称、操作名称)来询问何时发出请求。简而言之,URL 地址。我试过这些。
Example:
- Trial (AreaRegistration)
- Trial1Controller
- Home ActionResult >>> /Trial/Trial1/Home/
- A ActionResult >>> /Trial/Trial1/A/
- B ActionResult >>> /Trial/Trial1/B/
- Trial2Controller
- Examp (AreaRegistration)
- Examp1Controller
- Home ActionResult >>> /Examp/Examp1/Home/
- A ActionResult; >>> /Examp/Examp1/A/
- Examp2Controller
- Home ActionResult >>> /Examp/Examp2/Home/
var areas = Assembly.GetExecutingAssembly().GetTypes().Where(type => typeof(AreaRegistration).IsAssignableFrom(type)).ToList();
foreach(var area in areas)
{
var controllers = Assembly.GetExecutingAssembly().GetTypes().Where(type => typeof(Controller).IsAssignableFrom(type)).ToList();
foreach (var controller in controllers)
{
var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (var method in methods)
{
if (method.ReturnType == typeof(ActionResult))
{
lstControllerActions.Add(string.Format("Area -> Controller -> Action : {0} -> {1} -> {2}", area.Name, controller.Name, method.Name));
}
}
}
}
lstControllerActions Result:
- Area -> Controller -> Action : Trial -> Trial1 -> Home
- Area -> Controller -> Action : Trial -> Trial1 -> A
- Area -> Controller -> Action : Trial -> Trial1 -> B
- Area -> Controller -> Action : Trial -> Examp1 -> Home
- Area -> Controller -> Action : Trial -> Examp1 -> A
- Area -> Controller -> Action : Trial -> Examp2 -> Home
- Area -> Controller -> Action : Examp-> Trial1 -> Home
- Area -> Controller -> Action : Trial -> Trial1 -> A
- Area -> Controller -> Action : Trial -> Trial1 -> B
- Area -> Controller -> Action : Trial -> Examp1 -> Home
- Area -> Controller -> Action : Trial -> Examp1 -> A
- Area -> Controller -> Action : Trial -> Examp2 -> Home
一切都很美好,但是,区域之间没有连接控制器。
帮助
这个问题已经解决了。来自 @kamal-hamidi 的引述,谢谢。
public virtual ActionResult Index()
{
var list = GetSubClasses<Controller>();
// Get all controllers with their actions
var getAllcontrollers = (from item in list
let name = item.Name
where !item.Name.StartsWith("T4MVC_") // I'm using T4MVC
select new MyController()
{
Name = name.Replace("Controller", ""), Namespace = item.Namespace, MyActions = GetListOfAction(item)
}).ToList();
// Now we will get all areas that has been registered in route collection
var getAllAreas = RouteTable.Routes.OfType<Route>()
.Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
.Select(
r =>
new MyArea
{
Name = r.DataTokens["area"].ToString(),
Namespace = r.DataTokens["Namespaces"] as IList<string>,
}).ToList().GroupBy(x => x.Name).Select(g => g.First()).ToList();
// Add a new area for default controllers
getAllAreas.Insert(0, new MyArea()
{
Name = "Main",
Namespace = new List<string>()
{
typeof (Web.Controllers.HomeController).Namespace
}
});
foreach (var area in getAllAreas)
{
var temp = new List<MyController>();
foreach (var item in area.Namespace)
{
temp.AddRange(getAllcontrollers.Where(x => x.Namespace == item).ToList());
}
area.MyControllers = temp;
}
return View(getAllAreas);
}
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
private IEnumerable<MyAction> GetListOfAction(Type controller)
{
var navItems = new List<MyAction>();
// Get a descriptor of this controller
ReflectedControllerDescriptor controllerDesc = new ReflectedControllerDescriptor(controller);
// Look at each action in the controller
foreach (ActionDescriptor action in controllerDesc.GetCanonicalActions())
{
bool validAction = true;
bool isHttpPost = false;
// Get any attributes (filters) on the action
object[] attributes = action.GetCustomAttributes(false);
// Look at each attribute
foreach (object filter in attributes)
{
// Can we navigate to the action?
if (filter is ChildActionOnlyAttribute)
{
validAction = false;
break;
}
if (filter is HttpPostAttribute)
{
isHttpPost = true;
}
}
// Add the action to the list if it's "valid"
if (validAction)
navItems.Add(new MyAction()
{
Name = action.ActionName,
IsHttpPost = isHttpPost
});
}
return navItems;
}
public class MyAction
{
public string Name { get; set; }
public bool IsHttpPost { get; set; }
}
public class MyController
{
public string Name { get; set; }
public string Namespace { get; set; }
public IEnumerable<MyAction> MyActions { get; set; }
}
public class MyArea
{
public string Name { get; set; }
public IEnumerable<string> Namespace { get; set; }
public IEnumerable<MyController> MyControllers { get; set; }
}
它可以是不同的方法。结果,我想从应用程序中获取所有 url。
我计划在我的应用程序中授权。而且,我需要这些(区域名称、控制器名称、操作名称)来询问何时发出请求。简而言之,URL 地址。我试过这些。
Example:
- Trial (AreaRegistration)
- Trial1Controller
- Home ActionResult >>> /Trial/Trial1/Home/
- A ActionResult >>> /Trial/Trial1/A/
- B ActionResult >>> /Trial/Trial1/B/
- Trial2Controller
- Examp (AreaRegistration)
- Examp1Controller
- Home ActionResult >>> /Examp/Examp1/Home/
- A ActionResult; >>> /Examp/Examp1/A/
- Examp2Controller
- Home ActionResult >>> /Examp/Examp2/Home/
var areas = Assembly.GetExecutingAssembly().GetTypes().Where(type => typeof(AreaRegistration).IsAssignableFrom(type)).ToList();
foreach(var area in areas)
{
var controllers = Assembly.GetExecutingAssembly().GetTypes().Where(type => typeof(Controller).IsAssignableFrom(type)).ToList();
foreach (var controller in controllers)
{
var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (var method in methods)
{
if (method.ReturnType == typeof(ActionResult))
{
lstControllerActions.Add(string.Format("Area -> Controller -> Action : {0} -> {1} -> {2}", area.Name, controller.Name, method.Name));
}
}
}
}
lstControllerActions Result:
- Area -> Controller -> Action : Trial -> Trial1 -> Home
- Area -> Controller -> Action : Trial -> Trial1 -> A
- Area -> Controller -> Action : Trial -> Trial1 -> B
- Area -> Controller -> Action : Trial -> Examp1 -> Home
- Area -> Controller -> Action : Trial -> Examp1 -> A
- Area -> Controller -> Action : Trial -> Examp2 -> Home
- Area -> Controller -> Action : Examp-> Trial1 -> Home
- Area -> Controller -> Action : Trial -> Trial1 -> A
- Area -> Controller -> Action : Trial -> Trial1 -> B
- Area -> Controller -> Action : Trial -> Examp1 -> Home
- Area -> Controller -> Action : Trial -> Examp1 -> A
- Area -> Controller -> Action : Trial -> Examp2 -> Home
一切都很美好,但是,区域之间没有连接控制器。
帮助
这个问题已经解决了。来自 @kamal-hamidi 的引述,谢谢。
public virtual ActionResult Index()
{
var list = GetSubClasses<Controller>();
// Get all controllers with their actions
var getAllcontrollers = (from item in list
let name = item.Name
where !item.Name.StartsWith("T4MVC_") // I'm using T4MVC
select new MyController()
{
Name = name.Replace("Controller", ""), Namespace = item.Namespace, MyActions = GetListOfAction(item)
}).ToList();
// Now we will get all areas that has been registered in route collection
var getAllAreas = RouteTable.Routes.OfType<Route>()
.Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
.Select(
r =>
new MyArea
{
Name = r.DataTokens["area"].ToString(),
Namespace = r.DataTokens["Namespaces"] as IList<string>,
}).ToList().GroupBy(x => x.Name).Select(g => g.First()).ToList();
// Add a new area for default controllers
getAllAreas.Insert(0, new MyArea()
{
Name = "Main",
Namespace = new List<string>()
{
typeof (Web.Controllers.HomeController).Namespace
}
});
foreach (var area in getAllAreas)
{
var temp = new List<MyController>();
foreach (var item in area.Namespace)
{
temp.AddRange(getAllcontrollers.Where(x => x.Namespace == item).ToList());
}
area.MyControllers = temp;
}
return View(getAllAreas);
}
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
private IEnumerable<MyAction> GetListOfAction(Type controller)
{
var navItems = new List<MyAction>();
// Get a descriptor of this controller
ReflectedControllerDescriptor controllerDesc = new ReflectedControllerDescriptor(controller);
// Look at each action in the controller
foreach (ActionDescriptor action in controllerDesc.GetCanonicalActions())
{
bool validAction = true;
bool isHttpPost = false;
// Get any attributes (filters) on the action
object[] attributes = action.GetCustomAttributes(false);
// Look at each attribute
foreach (object filter in attributes)
{
// Can we navigate to the action?
if (filter is ChildActionOnlyAttribute)
{
validAction = false;
break;
}
if (filter is HttpPostAttribute)
{
isHttpPost = true;
}
}
// Add the action to the list if it's "valid"
if (validAction)
navItems.Add(new MyAction()
{
Name = action.ActionName,
IsHttpPost = isHttpPost
});
}
return navItems;
}
public class MyAction
{
public string Name { get; set; }
public bool IsHttpPost { get; set; }
}
public class MyController
{
public string Name { get; set; }
public string Namespace { get; set; }
public IEnumerable<MyAction> MyActions { get; set; }
}
public class MyArea
{
public string Name { get; set; }
public IEnumerable<string> Namespace { get; set; }
public IEnumerable<MyController> MyControllers { get; set; }
}