0

I'm using MVC3 and I need to deploy the same website to multiple locations, but with certain controllers disabled. The controllers to disable share the same base controller class.

I think I should be able to do this using different build configurations with each configuration having a different set of compiler switches to control which sets of controllers are enabled or disabled.

I'm having trouble determining the best way to achieve this. Can anyone help? Perhaps something in OnActionExecuting?

Thanks

enashnash
  • 1,578
  • 1
  • 15
  • 36

2 Answers2

0

Are you talking about conditional compilation depending on active configuration?

Go to project properties -> Build -> Conditional Compilation Symbols and add your string constant

Then you can write code like this:

#if TESTCFGACTIVE
        Console.WriteLine("Test CFG Constant");
#else
        Console.WriteLine("Normal");
#endif
        Console.ReadLine();

Running this in Release mode and Debug mode will yield different output, obviously.

Not entirely sure if this is what you're after, but I use it, sparingly, for things like disabling and enabling debugging/logging in different environments.

For something like what you're talking about, it's more about the behaviour of the app, I would use a simple config setting to turn on or off the controller. You can then use a build script to deploy the relevant config file to the relevant output directory.

Mel Padden
  • 983
  • 1
  • 9
  • 21
  • Yes, I'm aware of this, my question was really about how to disable a controller from its base class, not how conditional compilation works. – enashnash Apr 13 '12 at 12:54
0

Maybe by Creating custom route constraint and using ConditionalAttribute?

Something along these lines:

public class DisabledControllersRouteConstraint : IRouteConstraint
{
    IList<string> DisabledControllers = new List<string>();

    public DisabledControllersRouteConstraint()
    {
        DisableConstrollersDebug();
        DisableConstrollersRelease();
        DisableConstrollersProduction();
    }

    [Conditional("RELEASE")]
    private void DisableConstrollersRelease()
    {
        DisabledControllers.Add("ControllerDisabledForRelease");
    }

    [Conditional("PROD")]
    private void DisableConstrollersProduction()
    {
        DisabledControllers.Add("ControllerDisabledForProduction");
    }

    [Conditional("DEBUG")]
    private void DisableConstrollersDebug()
    {
        DisabledControllers.Add("ControllerDisabledForDebug");
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var controller = values["controller"];

        return !DisabledControllers.Contains(controller);
    }
}
archil
  • 39,013
  • 7
  • 65
  • 82
  • What problem do you see with base controller? If you add up more details, I would try to help you – archil Apr 13 '12 at 13:47
  • As I said in the question, the controllers to disable all share the same base controller. Doing it the way you describe would require me to keep a list of controllers that inherit from the base class. Not to mention using strings to describe them. It looks like I can do it by either using the `Conditional` attribute on the base class (if that works) or by simply using reflection to find the inheritors of the base controller `Type` to create my list. – enashnash Apr 13 '12 at 14:15
  • You cannot use ConditionalAttribute on controller classes, only on types that derive from Attribute. And my code is just the sample, you can simply customize it to use reflection when disabling controllers. String is used because is is the way routing works - with strings – archil Apr 13 '12 at 14:22