MVC exclude controller usage

久未见 提交于 2021-01-01 04:41:59

问题


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.

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.

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.

Is my only option to add a custom route each time we need to override? Or are there better solutions for this kind of problem ( custom MVC controller factory? )? + how do I define such routing?

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.

Kind regards,

Yannick


回答1:


You can go with controller inheritance.

ASP.NET core

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;
    }
}

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 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;


来源:https://stackoverflow.com/questions/48783934/mvc-exclude-controller-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!