Question

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

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top