获得与MVC项目相关领域 [英] Get Areas associated with an MVC project

查看:148
本文介绍了获得与MVC项目相关领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,我可以得到区域的名称在MVC项目?

Is there a way I can get the names of the areas in an MVC project?

这是我能想到的几种方法:

Here's a few ways I can think of:

a)如果我有来源,我可以通过该项目的文件夹结构浏览和地区文件夹下列举的文件夹。但是,也不能保证所有的文件夹重新present区域,除非我也列举了控制器和下的每个子文件夹视图文件夹中。这种方法,因为你可以告诉,很烂。

a) If I had the source, I could browse through the project folder structure and enumerate the folders under the Areas folder. But that wouldn't guarantee all the folders represent areas unless I also enumerated the Controllers and Views folder under each of the sub-folders. This approach, as you can tell, sucks.

二)从二进制,我可以列举符合条件 RootNamespaceOfProject.Areas所有的命名空间。*

b) From the binary, I could enumerate all namespaces that match the criteria RootNamespaceOfProject.Areas.*.

或者,我肯定有一个更优雅的方式。必须有在ASP.NET MVC框架,使所有地区的记录一些字典。

Or, I am sure there's a more elegant way. There must be some dictionary in the ASP.NET MVC framework that keeps a record of all the areas.

其次,有也是在MVC框架,重新presents面积的纲领性结构?我似乎无法找到一个。只有四个被的相关的的地区结构:

Secondly, is there also a programmatic construct in the MVC framework that represents an area? I can't seem to find one. There are only four constructs that are related to areas:

 1. AreaRegistration
 2. AreaRegistrationContext
 3. IRouteWithArea
 4. AreaHelpers (an internal class)

如果有之一,这样才有可能,比如,枚举区域内的所有控制器?

If there were one, would it be possible, say, to enumerate all the controllers within that area?

编辑

我只注意到有此文件名为 MVC-AreaRegistrationTypeCache.xml 文件夹\\ WINDOWS \\ Microsoft.NET \\框架\\ V4.XX \\临时ASP.NET文件中\\ ROOT \\ RandomlyGeneratedHash1 \\ RandomlyGeneratedHash2 \\ UserCache。

I just noticed that there's this file called MVC-AreaRegistrationTypeCache.xml in the folder \Windows\Microsoft.NET\Framework\v4.x.x\Temporary ASP.NET Files\root\RandomlyGeneratedHash1\RandomlyGeneratedHash2\UserCache.

此文件夹中有两个文件:

This folder has two files:

A)的 MVC-AreaRegistrationTypeCache.xml :这个文件的所有领域中具有区域机器上所有的组件列表

a) MVC-AreaRegistrationTypeCache.xml: This file has the list of all the areas in all the assemblies on the machine that have areas.

B)的 MVC-ControllerTypeCache.xml :该文件列出了组件领域内的控制器

b) MVC-ControllerTypeCache.xml: This file lists the controllers within the areas of the assemblies.

现在,找出的唯一的事情是,如果有有MVC框架读取这些文件,如果在一个二进制存在一定的区域告诉我一些编程的方式。

Now, the only thing to find out is if there's some programmatic way to have the MVC framework read these files and tell me if a certain area exists in a binary.

我想到了 AreaRegistration 类可能是一个。进一步探索吧...

I am thinking that the AreaRegistration class might be the one. Exploring it further...

推荐答案

这似乎是你将能够检索您的项目中注册的路线的唯一途径是通过枚举项目为其继承AreaRegistration类型外,还有不出现任何反对私营或公营哪些乐曲当前注册的地区。

It would seem that the only way you will be able to retrieve the routes registered in your project is by enumerating the project for types which inherit AreaRegistration, there does not appear to be any object private or public which tracks currently registered areas.

龙解释如下...

一个障碍要记住这里是区域比任意的字符串和命名空间的列表之间的耦合多一点。当一个区域被注册它仅仅是延长了路由集合,用于通过独特的区域DataToken识别一些新的规则的应用。

One hurdle to keep in mind here is that areas are little more than a coupling between an arbitrary string and a list of namespaces. When an area is registered it is merely extending the route collection for the application with some new rules identifiable by a unique "area" DataToken.

如果你看一下过程登记的地区,必须从的 System.Web.Mvc.AreaRegistration 并重写的 RegisterArea()。 RegisterArea()接收 AreaRegistrationContext 定义的区域名称,路由集合和对象的状态,但如果你观察到的格式实现RegisterArea(),它返回void,并且无助于preserve上下文对象。如果你看一下这RegisterArea()被触发(反射)前运行code,你可以看到),它传递给RegisterArea(该AreaRegistrationContext对象从不永久跟踪更重要的是,

If you look at the process for registering an area, you must inherit from System.Web.Mvc.AreaRegistration and override RegisterArea(). RegisterArea() receives an AreaRegistrationContext which defines an area name, route collection and object state, but if you observe the format for implementing RegisterArea(), it returns void and does nothing to preserve the context object. What's more, if you look at the code which runs before RegisterArea() is fired (Reflector), you can see that the AreaRegistrationContext object which is passed to RegisterArea() is never permanently tracked.

internal static void RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state)
{
    foreach (Type type in TypeCacheUtil.GetFilteredTypesFromAssemblies("MVC-AreaRegistrationTypeCache.xml", new Predicate<Type>(AreaRegistration.IsAreaRegistrationType), buildManager))
    {
        ((AreaRegistration) Activator.CreateInstance(type)).CreateContextAndRegister(routes, state);
    }
}

internal void CreateContextAndRegister(RouteCollection routes, object state)
{
    AreaRegistrationContext context = new AreaRegistrationContext(this.AreaName, routes, state);
    string str = base.GetType().Namespace;
    if (str != null)
    {
        context.Namespaces.Add(str + ".*");
    }
    this.RegisterArea(context);
}

正如你所看到的,静态的方法 RegisterAllAreas()的调用调用内部 RegisterAllAreas(RouteCollection路线,IBuildManager buildManager,对象状态),然后调用内部 CreateContextAndRegister(RouteCollection路线,对象状态),它创建AreaRegistrationContext并将其传递给 RegisterArea()

As you can see, a call to the static method RegisterAllAreas() invokes the internal RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state), which then calls the internal CreateContextAndRegister(RouteCollection routes, object state), which creates the AreaRegistrationContext and passes it to RegisterArea().

据我所知,从来没有在任何一点上,是永久保存每个区域创建的AreaRegistrationContext。

As far as I can tell, never, at any point, is the AreaRegistrationContext created for each area stored permanently.

这篇关于获得与MVC项目相关领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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