Poku
Poku

Reputation: 3188

Check if controller is part of specific "controller group"

In ASP.NET MVC im overwriting the OnException, so I can do some custom error handling on exceptions. One thing I would like to do here is to log from what area in the webapplication the error happend.

For example we have Webshop and Administration areas in the webapplications. Then I would like to identify if the exception comes from a Webshop controller or an Administration controller. This is what I have:

protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Exception != null)
            {
                ErrorTargetType targetErrorType = ErrorTargetType.DipService;

                if (filterContext.Controller is CatalogController)
                    targetErrorType = ErrorTargetType.WebshopInterface;

                LogException(filterContext.Exception, targetErrorType);
            }
            base.OnException(filterContext);
        }

It works fine but I would like do the if statement on a collection of Administration controllers insted. Does ASP.NET MVC have some standard functionality which provides all the controllers in the webapplication in a collection, if so, how do I separate the Webshop controllers from the Administration controllers?

Upvotes: 0

Views: 139

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

all the controllers in the webapplication in a collection

A collection of controllers or controllers group is a notion that doesn't exist in ASP.NET MVC nor it makes much sense. The fact that you call ControllerA, ControllerB and ControllerC being part of Administration is a notion that only you have defined. The sole notion of Administration hardly makes any sense in ASP.NET MVC. In ASP.NET MVC you have Models, Views and of course Controllers.

if so, how do i seperate the Webshop controllers from the Administration controllers?

Make them derive from a common base controller or make them implement a marker interface.

Upvotes: 3

Related Questions