How to determine Controller's Area in ASP.NET MVC
I have an ActionDescriptor from which I retrieve information about an
action and its controller:
ActionDescriptor desc = ...;
string action = desc.ActionName;
string controller = desc.ControllerDescriptor.ControllerName;
string area = ?;
I'm wondering if there is a better way to determine the controller's area
than having to parse its namespace, which I'm currently doing like so:
// e.g., Company.Areas.Foo.Controllers
var parts =
desc.ControllerDescriptor.ControllerType.Namespace.Split('.').ToList();
var areaIndex = parts.IndexOf("Areas");
if (areaIndex > -1) area = parts[areaIndex + 1];
// area = "Foo"
No comments:
Post a Comment