2

Ok basically I am building a nuget package, and one of the things i would like the class to do is get the current applications controllers

So at the moment in the main project I do the following

using SolutionName.Website.MVC5.Controllers;

What I want to be able to do is something like

using this.Application.Controllers;

So it dynamicaly fills in the namespace for whatever solution the package is installed to.

I have sat here for an hour going through the possible permutations and have also googled a fair bit but not sure exactly what to google for

This is including API Controllers and I am Using MVC 5

Cheers

Martyn

2 Answers 2

2

The following little piece of reflection will give you all the namespaces for files that end in "Controller".

var namespaces = this.GetType().Assembly.GetTypes()
    .Where(t => t.Name.EndsWith("Controller"))
    .Select(x => x.Namespace).Distinct().ToList();

You would need to call this from within your code, preferably in the Global.asax.

Keep in mind that your controllers may be scattered across a number of namespaces, so additional logic will have to account for that. The number of namespaces is solely determined by how rigidly you structure your applications.

Alternatively, you can also pull types that inherit directly from 'System.Web.Mvc.Controller', as pointed out by Andrew Whitaker.

var namespaces = this.GetType().Assembly.GetTypes()
    .Where(t => t => t.IsSubclassOf(typeof(Controller)))
    .Select(x => x.Namespace).Distinct().ToList();
Sign up to request clarification or add additional context in comments.

5 Comments

You could also grab all classes inheriting from System.Web.Mvc.Controller or System.Web.Mvc.ControllerBase
@AndrewWhitaker True, provided the OP isn't wanting to also potentially retrieve APIControllers and/or isn't working in ASP.NET 5. Both are assumptions I didn't want to make. That said, it's a valid alternative and I've updated my answer to represent it
HI guys just extended my question to include which and what mvc version
thank you @DavidL and AndrewWhitaker, David I have marked yours as the correct answer for my use case
@MartynWeber my pleasure and glad it works for you!. You may have upvoted instead of accepting. Please see meta.stackexchange.com/questions/23138/… and thank you for the accepted answer.
1

After using the information that @DavidL and @AndrewWhitaker gave me I thought I would post a little code snippet of how this can work in an MVC application as it maybe useful to go hand in hand with the question

I created the class below

public class GetControllerNameSpace
{
    public static string NamespaceTag; 
    public void GetControllerNameSpaceMethod()
    {
        var NamespaceTagList = this.GetType().Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Controller))).Select(x => x.Namespace).Distinct().ToList();
        NamespaceTag = NamespaceTagList.FirstOrDefault();
    }
}

and then in the RouteConfig.cs

using Controllers = GetControllerNameSpace;

This then lets me pass the information to my method here

    private static IEnumerable<Type> GetTypesWithFixedControllerRouteAttribute(RouteCollection routes)
            {
                                                         //This is where i am passing the variables
                foreach (Type type in Assembly.GetAssembly(typeof(Controllers.HomeController)).GetTypes())
                {
                    // Register any fixed actions
                    RegisterFixedActionRoutes(type, routes);

                    if (type.GetCustomAttributes(typeof(FixedControllerRouteAttribute), true).Any())
                    {
                        yield return type;
                    }
                }
            }

Please note that this works in my use case and may require more work when using multiple controller namespaces as mentioned in @DavidL's post

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.