Get the executing assembly of a web app referencing a class library

05 Sep 2013 .Net, C#, MVC No Comments

The Problem

I needed to check that any web app that was referencing my class library was giving me a valid controller and action name in some settings in its web.config, so that when I used them in a redirection there would be no errors because they did not exist.

When I used
to get the types I only got the types from the class library. I tried
but got null for GetEntryAssembly(). It seems this does not work for web apps.

This was another approach that did not work:

The Solution

I finally solved it with the help of StackExchange

To get the assembly:

Check if a controller with an action exists, using GetWebEntryAssembly()

Notes

  • This method checks that the controller name passed through is the full name, e.g. ‘HomeController’ as this is required.
  • The method does not require an action name to be passed in so that a controller’s existence can be verified without verifying an action name within it. This is useful as the controller and action are specified as separate app settings in the web.config, so I can verify the controller and then verify the action separately, also feeding in the controller.