MVC排除控制器使用情况 [英] MVC exclude controller usage
问题描述
我创建了一个具有10个控制器(部分类)的模板"项目A.所有这些控制器都打包在一个nuget程序包中,并由项目B,C和B消耗.D. nuget在 controllers/_core
文件夹中生成控制器,因此可以很好地将它们分开存储.nuget包没有具有对项目A的dll的引用.
I have created a 'template' project A which has 10 controllers ( partial class ).
All these controllers are packed in a nuget package and are consumed by projects B, C & D. The nuget generates the controllers in the folder controllers/_core
so they are nicely stored separately. The nuget package does not have a reference to the dll of project A.
现在要解决问题; 可能是所生成的控制器之一需要修改.通过添加第二部分,我们可以添加一些逻辑,但是可能需要覆盖现有方法以在该方法内部添加逻辑.因此(并且由于我在下面添加的原因),我正在考虑从这些基本控制器继承.当我们继承基本控制器时,它应该被MVC排除.
Now to get down to the problem; It could be that one of the controllers that are generated need to be modified. By adding a second partial we could add some logic, but it could be that we need to override an existing method to add logic inside the method. Because of this ( and because of the reason I added below ) I was thinking to inherit from these base controllers. When we inherit a base controller it should be excluded by MVC.
示例:Image PersonController是使用nuget包在controllers/_core文件夹中创建的,每种方法都是虚拟的.然后,我们将在从PersonController继承的controllers文件夹中创建一个 _PersonController
,为简单起见,仅覆盖index方法.此时,我们要更改路由,以便localhost/Person/index最终出现在 _PersonController
的index方法中,而不是 PersonController
的index方法中. PersonController
应该完全忽略.
Example: Image PersonController was created in the controllers/_core folder by using the nuget package and every method is virtual.
We will then create a _PersonController
in the controllers folder that inherits from PersonController and for simplicity sake only the index method is overriden. At this time we want to change the routing so localhost/Person/index ends up in the index method of the _PersonController
and not that of the PersonController
. PersonController
should be completely ignored.
我是否是每次需要覆盖时添加自定义路线的唯一选择?还是针对此类问题有更好的解决方案(自定义MVC控制器工厂?)?+如何定义这种路由?
我想继承的其他原因:每次我们更新nuget软件包时,它都会尝试覆盖对消费者项目B,C和Amp中生成的控制器所做的所有更改.D.
Extra reason why I was thinking to inherrit: Every time we do an update of the nuget package it'll try to override all the changes made to the controllers that were generated in the consumer projects B, C & D.
亲切的问候,
Yannick
推荐答案
您可以继承控制器继承.
You can go with controller inheritance.
要忽略不必要的控制器,您需要实现 IApplicationFeatureProvider< ControllerFeature>
或从 ControllerFeatureProvider
派生:
To ignore unnecessary controllers you need to implement IApplicationFeatureProvider<ControllerFeature>
or derive from ControllerFeatureProvider
:
public class MyControllerFeatureProvider : ControllerFeatureProvider
{
protected override bool IsController(TypeInfo typeInfo)
{
var isController = base.IsController(typeInfo);
if (isController)
{
//overriding isController value
}
return isController;
}
}
然后在您的 Starup.ConfigureServices
中,您需要替换默认的 ControllerFeatureProvider
:
Then at your Starup.ConfigureServices
you need to replace the default ControllerFeatureProvider
:
services.AddMvc()
.ConfigureApplicationPartManager(manager =>
{
var controllerFeatureProvider =
manager.FeatureProviders
.Single(p => p.GetType() == typeof(Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider));
manager.FeatureProviders[manager.FeatureProviders.IndexOf(controllerFeatureProvider)] =
new Attributes.MyControllerFeatureProvider();
});
ASP.NET MVC 5和更早版本
在特定情况下,可以在路由注册中使用名称空间列表,并将 UseNamespaceFallback
设置为 false
以忽略其他名称空间:
ASP.NET MVC 5 and earlier
In your particular case you can go with namespaces list on the route registration and setting UseNamespaceFallback
to false
to ignore other namespaces:
var myRoute = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new [] {"Namespace1", "Namespace2"}
);
myRoute.DataTokens["UseNamespaceFallback"] = false;
这篇关于MVC排除控制器使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!