Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

How do I use ControllerFeature instead of IControllerTypeProvider? #5004

Closed
marcuslindblom opened this issue Jul 12, 2016 · 4 comments
Closed

Comments

@marcuslindblom
Copy link

In RC1 I used the IControllerTypeProvider in my custom router to check if one controller has a specific action. In RC2 it seems to be removed and maybe replaced with ControllerFeature? I tried to replace IControllerTypeProvider with ControllerFeature but that guy is null.

This is how I used the IControllerTypeProvider and now I want to replace it with the ControllerFeature, I think?

public ControllerMapper(IControllerTypeProvider controllerTypeProvider)
{
  foreach (var type in controllerTypeProvider.ControllerTypes)
  {
    ControllerMap.TryAdd(type, type.Name.Replace("Controller", ""));
    var methodNames = type.GetMethods().Select(x => x.Name).ToArray();
    ControllerActionMap.TryAdd(type.Name.Replace("Controller", ""), methodNames);
  }
}
@rynowak
Copy link
Member

rynowak commented Jul 12, 2016

You can populate a ControllerFeature like so:
https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/Internal/ControllerActionDescriptorProvider.cs#L96 (DI a reference to ApplicationPartManager).

What is it you're trying to do exactly? Looking at the types of controllers is going to miss some of the metadata that affects how actions are mapped.

@marcuslindblom
Copy link
Author

@rynowak Ok, so I did this instead.

services.AddSingleton<ApplicationPartManager>();

Then in my ControllerMapper I change the constructor

public ControllerMapper(ApplicationPartManager manager) {
  var feature = new ControllerFeature();
  manager.PopulateFeature(feature);
  ...

But feature.Controllers is empty? If I have multiple assemblies with controllers I need to load them all.

To make a long story short, I'm developing a CMS (still prototype) and I use dynamic and, according to me more readable hierarchical urls not based on the the controller name e.g. /page-one/page-two/page-three/ and so on.

For this I use a custom router which is the first route in the list, this is how the lookup works. If the lookup find a a node with a controller, I would like to check if that action on that controller exists.

I would appreciate feedback on how I could improve routing code.

@rynowak
Copy link
Member

rynowak commented Jul 12, 2016

You shouldn't have to register the ApplicationPartManager - AddMvc() does that.

If you want to use ApplicationPartManager outside of calling AddMvc() then you'll have to decide how to populate the list of parts (assemblies).

@marcuslindblom
Copy link
Author

@rynowak Ok, changed it to this and it seems to work, at least with one assembly.

services.AddMvc().ConfigureApplicationPartManager(manager =>
            {
                var feature = new ControllerFeature();
                manager.PopulateFeature(feature);
                services.AddSingleton<IControllerMapper>(new ControllerMapper(feature));
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants