如何通过名称获取控制器类型[重复]

时间:2022-05-20 23:38:46

Possible Duplicate:
How can I get controller type and action info from a url or from route data?

可能重复:如何从URL或路由数据中获取控制器类型和操作信息?

I have a list of controller names and their actions names. What I need to do is to read values from custom attributes on those actions. The problem is that it seems there is no easy way to get controller types (Having that the rest is easy.) ASP.NET MVC framework has this functionality in DefaultControllerFactory, but it's not accessible outside of framework itself. It doesn't look like a good idea to reinvent the wheel and implement it myself, especially because MVC framework has cache for controller types, which I would need to duplicate. Is there any better solution?

我有一个控制器名称列表和他们的动作名称。我需要做的是从这些操作的自定义属性中读取值。问题是似乎没有简单的方法来获取控制器类型(其余部分很简单。)ASP.NET MVC框架在DefaultControllerFactory中具有此功能,但它不能在框架本身之外访问。重新发明*并自己实现它似乎不是一个好主意,特别是因为MVC框架具有控制器类型的缓存,我需要复制它。有没有更好的解决方案?


Upd. Accessing the list of Controllers/Actions in an ASP.NET MVC application describes a similar, but different problem. I don't need to determine what controllers/actions are available to be executed, I need to get type of a single controller.

UPD。访问ASP.NET MVC应用程序中的控制器/操作列表描述了一个类似但不同的问题。我不需要确定可以执行哪些控制器/操作,我需要获取单个控制器的类型。

2 个解决方案

#1


I'm not really clear on where you need to do this. If you need to do this inside your MVC app then you could implement your own controller factory and put your code in there, storing the results with a cache keyed on controller type - e.g.:

我不清楚你需要做什么。如果您需要在MVC应用程序中执行此操作,那么您可以实现自己的控制器工厂并将代码放在那里,使用键控在控制器类型上的缓存存储结果 - 例如:

public class CustomControllerFactory : DefaultControllerFactory
{
  protected override IController GetControllerInstance(Type controllerType)
  {
    // do something with controller attributes on controllerType here the
    // first time a controller is seen and store the results in a
    // static cache keyed on the controller type

    return base.GetControllerInstance(controllerType);
  }
}

and then register that ControllerFactory on application startup (e.g. in global.asax):

然后在应用程序启动时注册ControllerFactory(例如在global.asax中):

ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());

Alternatively, you could simply use reflection to look for all classes in your assembly that end with "Controller" and run your code on there - again, I'm unclear on where you need this to happen.

或者,你可以简单地使用反射来查找程序集中以“Controller”结尾的所有类并在那里运行你的代码 - 再次,我不清楚你需要在哪里发生这种情况。

#2


Since you already have a list of typenames, using reflection to rip through the assembly, retrieve the type, and iterate the actions, checking for the existence of the interesting attribute, is probably the most direct method. I haven't done enough crawling around in ASP.Net MVC to know if there might be a way to extract the type information from the controller cache in MVC...

由于您已经有了一个类型名列表,使用反射来翻阅程序集,检索类型并迭代这些操作,检查是否存在有趣的属性,这可能是最直接的方法。我还没有在ASP.Net MVC中做足够的爬行来知道是否有办法从MVC中的控制器缓存中提取类型信息...

Alternatively, if you can ask the cache for an object of each controller type (and ASP.Net MVC controllers, or more specifically your controllers, are cheap enough to construct, or are constructed and cached, an unknown for me), retrieving each of the controllers in the system and retrieving their type might be more direct.

或者,如果您可以向缓存请求每个控制器类型的对象(以及ASP.Net MVC控制器,或者更具体地说是您的控制器,它们足够便宜,可以构造,或构建和缓存,对我来说是未知的),检索每个控制器类型系统中的控制器和检索它们的类型可能更直接。

#1


I'm not really clear on where you need to do this. If you need to do this inside your MVC app then you could implement your own controller factory and put your code in there, storing the results with a cache keyed on controller type - e.g.:

我不清楚你需要做什么。如果您需要在MVC应用程序中执行此操作,那么您可以实现自己的控制器工厂并将代码放在那里,使用键控在控制器类型上的缓存存储结果 - 例如:

public class CustomControllerFactory : DefaultControllerFactory
{
  protected override IController GetControllerInstance(Type controllerType)
  {
    // do something with controller attributes on controllerType here the
    // first time a controller is seen and store the results in a
    // static cache keyed on the controller type

    return base.GetControllerInstance(controllerType);
  }
}

and then register that ControllerFactory on application startup (e.g. in global.asax):

然后在应用程序启动时注册ControllerFactory(例如在global.asax中):

ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());

Alternatively, you could simply use reflection to look for all classes in your assembly that end with "Controller" and run your code on there - again, I'm unclear on where you need this to happen.

或者,你可以简单地使用反射来查找程序集中以“Controller”结尾的所有类并在那里运行你的代码 - 再次,我不清楚你需要在哪里发生这种情况。

#2


Since you already have a list of typenames, using reflection to rip through the assembly, retrieve the type, and iterate the actions, checking for the existence of the interesting attribute, is probably the most direct method. I haven't done enough crawling around in ASP.Net MVC to know if there might be a way to extract the type information from the controller cache in MVC...

由于您已经有了一个类型名列表,使用反射来翻阅程序集,检索类型并迭代这些操作,检查是否存在有趣的属性,这可能是最直接的方法。我还没有在ASP.Net MVC中做足够的爬行来知道是否有办法从MVC中的控制器缓存中提取类型信息...

Alternatively, if you can ask the cache for an object of each controller type (and ASP.Net MVC controllers, or more specifically your controllers, are cheap enough to construct, or are constructed and cached, an unknown for me), retrieving each of the controllers in the system and retrieving their type might be more direct.

或者,如果您可以向缓存请求每个控制器类型的对象(以及ASP.Net MVC控制器,或者更具体地说是您的控制器,它们足够便宜,可以构造,或构建和缓存,对我来说是未知的),检索每个控制器类型系统中的控制器和检索它们的类型可能更直接。