Monday, February 27, 2012

ASP.NET MVC 3: Call Controller from external assembly

I've recently encountered a task to have pluggable controllers so that you could change web application behavior without source code recompilation. The idea was to update an assembly in the /Bin folder, possibly put/update some views under web application root folder and get new behavior in place.

As it turned out ASP.NET MVC 3 standard controller factory is pretty smart and may automatically search for controllers in all assemblies under /Bin folders (under web app root) and load them - for the simple case described there's no need to use any IoC container, MEF or other 3rd parties.

In order to obtain a working external assembly controller you have to:
  • Create a class library and reference System.Web.Mvc
  • Add controller class and fill in logic
  • Put the outpy DLL to web application /Bin folder. Application will be restarted. While debugging you may reference the class library with the controller to get it automatically copied to /Bin folder.
  • Put the view file(s) for action(s) defined in the controller to web application folder.

Views for the actions within external controller will be resolved according to standard ASP.NET MVC rules and searched under /Views folder (until you start defining full view paths in action methods).

In case you have 2 DLL's in the been folder which contain controller classes with same names then default routes will fail and upon request to action within controller name non unique an error will occur. In order to resolve conflicts you need to amend route table in global.asax and explicitly pass namespaces to be used for controller classes resolution (assuming we have same controllers in ExternalAssembly.Controllers and ExternalAssembly2.Controllers the first will take precedence):

        public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Default", action = "Index", id = UrlParameter.Optional },
new[] {"ExternalAssembly.Controllers"}
);
}

Friday, February 24, 2012

Bilateral filter has been added to AForge.NET

Starting from version 2.2.4 AForge.NET contains Bilateral filter:
http://www.aforgenet.com/news/2012.02.23.releasing_framework_2.2.4.html

Use AForge.Imaging.Filters.BilateralSmoothing class.

Friday, February 10, 2012

Bilateral effect for Paint.NET

I've created and published Paint.NET effect plug-in implementing bilateral filtration. You may find it on Codeplex project page (so as installation instructions). Now you can easily use the filter while working with you photos and images.


More information on the filter and it's parameters can be found here.