AttributeRouting-在装配中获取路线的方法 [英] AttributeRouting - Method to get routes in assembly

查看:92
本文介绍了AttributeRouting-在装配中获取路线的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Orchard CMS中使用 AttributeRouting .为此,我需要使用返回果园RouteDescriptor s列表的方法来实现IRouteProvider.

I want to use AttributeRouting with Orchard CMS. To do so I need to implement an IRouteProvider with a method that returns an list of Orchard RouteDescriptors.

我需要一种获取路线列表的方法,以便可以执行以下操作:

I need a way to get the routes list so I can do something like this:

   public IEnumerable<RouteDescriptor> GetRoutes()
    {
        return _routes ?? (_routes = MvcRouting.GetRoutes(GetType().Assembly).Select(route => new RouteDescriptor
        {
            ...
        }).ToArray());
    }

此方法来自MvcRouting,但我想使用功能更丰富的 AttributeRouting ,但找不到扫描当前方法的方法路由属性的程序集.需要AttributeRouting返回我可以投影到RouteDescriptor列表中但未实际注册的路由列表的方法,然后将其留给Orchard.

This method is from MvcRouting but I want to use the more feature rich AttributeRouting but cannot find a way to scan current assembly for routing attributes. Need a way for AttributeRouting to return the list of routes that I can project into a list of RouteDescriptors but not actually registering them, leaving that to Orchard.

 public class RouteDescriptor {
    public string Name { get; set; }
    public int Priority { get; set; }
    public System.Web.Routing.RouteBase Route { get; set; }
    public System.Web.SessionState.SessionStateBehavior SessionState { get; set; }
}

如果我想直接注册路线(在非Orchard项目中),我将使用以下AttributeRouting扩展方法:

If I wanted to register the routes directly (in a non-Orchard project) I would use the following AttributeRouting extension methods:

  routesCollection.MapAttributeRoutes(config =>
        {
            config.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
        }

但是,这对于其他Orchard模块而言并不理想.因此需要采用上面的方法.

But this won't place nice with other Orchard modules. So need to take the approach above.

您能想到什么吗?

推荐答案

正如Bertrand所说,您需要的是对当前可用的Controller类型的简单反映.

As Bertrand already noted, what you need is a simple reflection over currently available Controller types.

  1. 像以前一样执行IRouteProvider.
  2. 获取当前启用的模块导出的所有Controller类型

IExtensionManager _extensions;
ShellBlueprint _shell;
...
var types = _extensions
                .LoadFeatures(_extensions.EnabledFeatures(_shell.Descriptor))
                    .SelectMany(feature => feature
                        .ExportedTypes
                        .Where(t => typeof(Controller).IsAssignableFrom(t)));

  • 遍历以上每种类型的方法,并选择已定义给定属性的方法.对于每个选择的方法,获取其名称(动作名称),包含其声明类型(区域名称)以及属性数据(路线模式等)的程序集名称.

  • Loop over methods of each of the types above and pick those that have a given attribute defined. For each of the methods picked, fetch its name (action name), name of the assembly that contains its declaring type (area name) along with the attribute data (route pattern etc.).

    这篇关于AttributeRouting-在装配中获取路线的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆