[英]Making a controller not visible to the client in ASP.NET WEB API (or MVC)
Let's suppose I have a layer of abstract controllers, which delegates the request to its child controller class, until it reaches the implementation. 假设我有一层抽象控制器,它将请求委托给其子控制器类,直到实现为止。
Think of it like a pipeline of controllers, that the request must go through, and includes caching the responses, authorizing and authenticating the user, validating the input and output data, handling repository access, etc. 认为它就像是控制器的管道,请求必须经过,并且包括缓存响应,授权和验证用户,验证输入和输出数据,处理存储库访问等。
My leaf class (the last child of the hierarchy), may have the following signature: 我的叶类(层次结构的最后一个孩子)可能具有以下签名:
public class SeasonsController : DefaultPersistenceRestController
<int, Season, SeasonPutDTO, SeasonPostDTO, SeasonQueryData> {
/** Controller implementation here **/
}
The base classes have a lot of reusable code located in one module, this is good and has helped me a lot when changing the logic of my controllers at a global level. 基类在一个模块中有很多可重用的代码,这很好,并且在全局范围内更改控制器的逻辑时对我有很大帮助。
Now, suppose SeasonsController
need to call EpisodesController
, for irrelevant reasons. 现在,假设
SeasonsController
出于不相关的原因需要调用EpisodesController
。
The call would be like this: 调用将是这样的:
EpisodesController episodeController = new EpisodesController();
//Do something with EpisodesController
The problem is that I don't want EpisodesController
to be accessed from the outside, such as client's request. 问题是我不希望从外部访问
EpisodesController
,例如客户的请求。 ASP.NET automatically identifies controllers and creates a public endpoint for them, such as http://localhost:80/episodes
. ASP.NET自动识别控制器并为它们创建一个公共端点,例如
http://localhost:80/episodes
。
I created EpisodesController
because it uses a lot of logic from the controller's base classes, but I intend to use it internally. 我创建了
EpisodesController
是因为它使用了控制器基类中的大量逻辑,但是我打算在内部使用它。
I can desactivate authentication, authorization, cache and all other stuff that will be useless if a controller is used in this way, so that's not a problem. 如果以这种方式使用控制器,我可以取消激活身份验证,授权,缓存和所有其他无用的东西,所以这不是问题。
However, I cannot manage to prevent ASP.NET to ignore my EpisodesController
class, and to not consider it like a controller. 但是,我无法防止ASP.NET忽略我的
EpisodesController
类,也不能将其视为控制器。
Is there an attribute or annotation maybe that will tell the compiler to do this? 是否存在可以告诉编译器执行此操作的属性或注释? Maybe some modification in Web.config ?.
也许在Web.config中进行一些修改?
Also note that I don't want to change EpisodesController
's class name to another name, as it is really a controller, but an internal one. 还要注意,我不想将
EpisodesController
的类名更改为另一个名称,因为它实际上是一个控制器,但是是一个内部控制器。
You could try to use the IgnoreRoute extension method. 您可以尝试使用IgnoreRoute扩展方法。 Or you could try the
internal
as suggested by beautifulcoder. 或者,您也可以按照beautifulcoder的建议尝试
internal
。 If it's in another assembly (and you can modify it) you could also make it visible to other assemblies with InternalsVisibleToAttribute . 如果它在另一个程序集中(并且您可以对其进行修改),则还可以使用InternalsVisibleToAttribute使它对其他程序可见。
Although to be honest, using one controller within another controller doesn't seem right to me. 虽然说实话,但在我看来,在另一个控制器中使用一个控制器似乎不合适。 I would try and refactor you common functionality to services/helpers, then you could probably also make your
EpisodesController
into a simple service. 我会尝试将您的常用功能重构为服务/助手,那么您可能还可以将
EpisodesController
变成一个简单的服务。 Composition over inheritance and all that :) 继承和其他方面的组成:)
If you make a controller public
it will be accessible. 如果将控制器
public
,则可以访问它。 From what I understand, you can change it to protected
or internal
. 据我了解,您可以将其更改为
protected
或internal
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.