Wednesday, October 31, 2012

Constraint on Action names in MVC routing parameter

Let's say we have Web-site named "example.com" and controller with 3 Action methods:
 public class DiscussionController : BaseController  
 {  
      ...  
    public ActionResult One(string url)  
    {  
      var model= DiscussionService.GetDiscussionByUrl(url);  
      return View(model);  
    }  
    public ActionResult All()  
    {  
      ...  
    }  
    public ActionResult Popular()  
    {  
      ...  
    }  
 }  

We also have 2 registered routes(custom and standart MVC route):
 public static void RegisterRoutes(RouteCollection routes)  
 {  
   routes.MapRoute(  
     "Discussion",  
     "discussion/{url}",  
      new { controller = "Discussion" , action = "One" }  
    );  
   routes.MapRoute(  
     "Default",  
     "{controller}/{action}/{id}",  
     new { controller = "Home" , action = "Index" , id = "" }  
    );  
 }  

If we open "example.com/discussion/to-be-on-not-to-be/" URL in browser then One() Action method fires with url="to-be-on-not-to-be". So, our 1st route works fine.

But if we try to fire All() method with "example.com/discussion/all/" URL, we get One() method called again with url="all". Problem is that our 1st route fires before default route.

One solution is to use custom Constraint to filter names of controller's Action methods from our custom route:
 public static void RegisterRoutes(RouteCollection routes)  
 {  
    routes.MapRoute(  
      "Discussion",  
      "discussion/{url}",  
      new { controller = "Discussion" , action = "One" },   
      new { url = new ControllerMethodsConstraint<DiscussionController>("url" ) }  
     );   
     routes.MapRoute(  
       "Default",  
       "{controller}/{action}/{id}",  
       new { controller = "Home" , action = "Index" , id = "" }  
     );        
 }  

This Constraint class looks like:
 public class ControllerMethodsConstraint<T>: IRouteConstraint where T : BaseController  
 {  
    private readonly static IList<string> _occupiedMethodsNames;  
    private readonly string _idParamName;  
    static ControllerMethodsConstraint()  
    {  
      _occupiedMethodsNames = typeof(T).GetMethods().Where(m => m.IsPublic && (typeof (ActionResult ).IsAssignableFrom(m.ReturnType))).Select(m => m.Name.ToLower()).ToArray();  
    }  
    public ControllerMethodsConstraint(string idParamName)  
    {  
      _idParamName = idParamName;  
    }  
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)  
    {  
      return !_occupiedMethodsNames.Contains(((string )values[_idParamName]).ToLower());  
    }  
 }  

This Constraint actually just check if current param(in our case it is "url") value not equals to Action method names of specified controller.
If there is no Action methods with that name, then route is approved. Otherwise - not, so, next(default) route will work.

Tuesday, October 30, 2012

Lowercase route example in ASP.NET MVC

I like default URL routing behaviour of ASP.NET MVC, but sometimes you want something special.

Let's say we want Lower Case URLs in our Web site. Here's an example of Global.asax:
 public class MvcApplication : System.Web.HttpApplication  
   {  
     public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
     {  
       filters.Add(new HandleErrorAttribute());  
     }  
     public static void RegisterRoutes(RouteCollection routes)  
     {  
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
       routes.MapLowercaseRoute(  
         "Default", // Route name  
         "{controller}/{action}/{id}", // URL with parameters  
         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults  
       );  
     }  
     protected void Application_Start()  
     {  
       AreaRegistration.RegisterAllAreas();  
       RegisterGlobalFilters(GlobalFilters.Filters);  
       RegisterRoutes(RouteTable.Routes);  
     }  
   }  

MapLowercaseRoute is an extension method for RouteCollection class. It's looks like:
 public static class RoutesEx  
   {  
     public static void MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults)  
     {  
       routes.MapLowercaseRoute(name, url, defaults, null);  
     }  
     public static void MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults,  
                      object constraints)  
     {  
       if (routes == null)  
         throw new ArgumentNullException("routes");  
       if (url == null)  
         throw new ArgumentNullException("url");  
       var route = new LowercaseRoute(url, new MvcRouteHandler())  
       {  
         Defaults = new RouteValueDictionary(defaults),  
         Constraints = new RouteValueDictionary(constraints)  
       };  
       if (String.IsNullOrEmpty(name))  
         routes.Add(route);  
       else  
         routes.Add(name, route);  
     }      
   }  

The key point is LowercaseRoute class that does lowercasing:
 public class LowercaseRoute : Route  
   {  
     public LowercaseRoute(string url, IRouteHandler routeHandler)  
       : base(url, routeHandler) { }  
     public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)  
       : base(url, defaults, routeHandler) { }  
     public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)  
       : base(url, defaults, constraints, routeHandler) { }  
     public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) { }  
     public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)  
     {  
       var path = base.GetVirtualPath(requestContext, values);  
       if (path != null)  
         path.VirtualPath = path.VirtualPath.ToLowerInvariant();  
       return path;  
     }  
   }  

As you see, it's very easy to make some elaborations to default ASP.NET MVC routing machinery.

Monday, October 29, 2012

Defining methods in Razor syntax

Sometimes it's useful to use methods in Razor views. I mean not @helper methods that write out some markup, but methods that return any type you want.

Here's an example of controller, action, and corresponded Razor view template:
 public class HomeController: Controller  
 {  
   public ActionResult Index()  
   {  
    return View();  
   }  
 }  

Views/Home/Index.cshtml:
 <html>  
 <body>  
 @functions {  
  public int DoSomethingAndReturnResult(int someValue)  
  {  
   var result= someValue*2;  
   return result;  
   }  
 }  
 @{  
   var computedValue1 = DoSomethingAndReturnResult(2);  
   var computedValue2 = DoSomethingAndReturnResult(5);  
   <div >  
    @(computedValue1+computedValue2)  
   </div >  
  }  
 </body>  
 </html>  

As you see, we can use @functions syntax to define areas with methods, that we can use many times for some plain UI-purposes. I say "plain UI-purposes ", because we must remember that Views are bad place to put significant logic into.

Friday, October 26, 2012

Writing custom Windsor Controller Factory in ASP.NET MVC

Let's say we use Windsor IoC container in our ASP.NET MVC3 application. We registered some dependencies in Global.asax bootstrapper and now we need somehow to resolve them in our controller.

Of course we can do this:
 public class MyController : BaseController  
 {   
   private IFoo _dependency;   
   public MyController()  
   {  
    _dependency = Container.Resolve<IFoo>();   
   }  
   public ActionResult Index()  
   {   
    _dependency.DoSomething(); 
     ... 
   }  
 }  

But it is not good solution, because now we have dependency on IoC container itself in our controller. Besides, using any forms of Service Locator is a bad design practice.

More elegant solution is to write custom Controller Factory(in our case it is Windsor Controller Factory):
 public class WindsorControllerFactory : DefaultControllerFactory  
 {  
   private readonly IKernel _kernel;  
   public WindsorControllerFactory(IKernel kernel)  
   {  
    _kernel = kernel;  
   }  
   public override void ReleaseController(IController controller)  
   {  
    _kernel.ReleaseComponent(controller);  
   }  
   protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)  
   {  
    if (controllerType == null)  
    {  
      throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));  
    }  
    var controller = (Controller)_kernel.Resolve(controllerType);      
    return controller;  
   }  
 }  

And then to register this factory as default Controller Factory in our MVC application in Global.asax:
 Container = new WindsorContainer();  
 ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container.Kernel));  

Now we can do elegant dependency injections in our controller's constructor without references on specific IoC(in our case it's Castle Windsor):
 public class MyController : BaseController  
 {  
   private IFoo _dependency;  
   public MyController(IFoo dependency)  
   {  
    _dependency = dependency;  
   }  
   public ActionResult Index()  
   {  
    _dependency.DoSomething();
     ...
   }  
 }  

Monday, October 22, 2012

Cookie value is nulled when using Response.Cookies

I have the following code to check if Cookie with name "city" already exists in Response.Cookies collection:
 if (HttpContext.Current.Response.Cookies["city"]!=null)   
   return HttpUtility.UrlDecode(HttpContext.Current.Response.Cookies["city"].Value);  

But to my big surprise this code is not working in that way that i expected. This code resets Cookie value to NULL when i am trying to check if Cookie value is not NULL.

Here the code that works fine for me:
 if (HttpContext.Current.Response.Cookies.AllKeys.Contains("city"))  
   return HttpUtility.UrlDecode(HttpContext.Current.Response.Cookies["city"].Value);  


Thursday, October 18, 2012

Dependency injection in ASP.NET MVC 3 validation attributes

In previous article(Dependency injection in ASP.NET MVC 3 action filters) we have seen how to inject dependencies in MVC Action Filters. Now let's try to inject dependency in Validation attribute. Validation attributes is used in MVC Model Validation. There are many useful built-in attributes, but often you need to write your own.

Let's imagine that we need attribute that validate that user's login(typed in registration form) is not occupied. So we write custom attribute:
 public class LoginExistanceValidatorAttribute : ValidationAttribute  
 {  
   public IRegistrator Registrator { get ; set ; }  
   public override bool IsValid(object value)  
   {  
      var valueStr = value as string;  
      if (string.IsNullOrEmpty(valueStr))  
       return false;  
      return Registrator.CheckLoginExistance(valueStr);  
    }  
 }  

Our attribute needs IRegistrator dependency to be injected. So, how to inject it? We have problem like one than we have discussed in previous article with Action Filters. And we have similar solution(as before, we use Castle Windsor IoC container):
 var container = new WindsorContainer();   
 container.Register(Component.For<ModelValidator>().ImplementedBy<WindsorModelValidator>().LifeStyle.Transient);   
 DataAnnotationsModelValidatorProvider.RegisterDefaultAdapterFactory((metadata, context, attribute) => container.Resolve<ModelValidator>(new { metadata, context, attribute }));  

All we need is to write custom Model Validator WindsorModelValidator that will inject dependencies in Validation attribute at some point. Example:
 public class WindsorModelValidator : DataAnnotationsModelValidator  
 {  
   public IWindsorContainer Container { get ; set ; }  
   public WindsorModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute): base(metadata, context, attribute)
   {  
   }  
   public override IEnumerable<ModelValidationResult> Validate(object container)  
   {  
     // inject the services from the container that  
     // the Validation attribute requires  
     Container.Kernel.InjectProperties(Attribute);  
     ValidationContext context = CreateValidationContext(container);  
     ValidationResult result = Attribute.GetValidationResult(Metadata.Model, context);  
     if (result != ValidationResult.Success)  
     {  
       yield return new ModelValidationResult  
       {  
         Message = result.ErrorMessage  
       };  
     }  
   }  
   protected virtual ValidationContext CreateValidationContext(object container)  
   {  
     var context = new ValidationContext(container ?? Metadata.Model, new WindsorServiceProvider(Container), null );  
     context.DisplayName = Metadata.GetDisplayName();  
     return context;  
    }  
 }  

All magic is in this line:
 Container.Kernel.InjectProperties(Attribute);  

We again use this extension method to inject dependencies in object. You can view code of this method in previous article about Action filters.

Wednesday, October 17, 2012

Dependency injection in ASP.NET MVC 3 action filters

So far it's not easy task to inject dependencies in MVC 3 action filters.

For example, we have action filter attribute that performs redirection to Login page if user is not authorized:
 public class AuthorizeAttribute : ActionFilterAttribute  
 {  
   public ISiteUser SiteUser { get; set; }   
   public override void OnActionExecuting( ActionExecutingContext filterContext)  
   {  
     if (!SiteUser.IsAuthenticated)  
     {  
       var urlHelper = new UrlHelper(HttpContext .Current.Request.RequestContext);  
       if (urlHelper.RequestContext.HttpContext.Request.Url != null)  
       {  
        filterContext.Result = new RedirectResult(urlHelper.Action("Login" , "Authentication" , new { ReturnUrl = urlHelper.RequestContext.HttpContext.Request.Url.PathAndQuery }));   
       }   
     }  
   }   
 }  

This action filter has dependency ISiteUser. So, how to inject concrete implementation of SiteUser there? It is good design practice to inject dependencies in constructor. But at this point we do not control moment when this attribute is created and we do not have any way
to catch this moment.

What we can to do is to write custom ActionInvoker(using Castle Windsor IoC container, for example) and to step in process of controller action invocation:
 public class WindsorActionInvoker : ControllerActionInvoker  
 {  
   private readonly IKernel _kernel;  
   public WindsorActionInvoker(IKernel kernel)  
   {  
     _kernel = kernel;  
   }  
   protected override ActionExecutedContext InvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter > filters, ActionDescriptor actionDescriptor, IDictionary<string , object > parameters)  
   {  
     foreach (IActionFilter actionFilter in filters)  
     {  
      _kernel.InjectProperties(actionFilter);  
     }  
     return base.InvokeActionMethodWithFilters(controllerContext, filters, actionDescriptor, parameters);  
   }  
 }  

As you can see, we iterate through controller action filters and call InjectProperties extension method which magically performs dependency injections.

Let's look at this method:
 public static class WindsorExtensions  
 {  
   public static void InjectProperties(this IKernel kernel, object target)  
   {  
     var type = target.GetType();  
     foreach (var property in type.GetProperties( BindingFlags.Public | BindingFlags.Instance))  
     {  
      if (property.CanWrite && kernel.HasComponent(property.PropertyType))  
      {  
        var value = kernel.Resolve(property.PropertyType);  
        try  
        {  
          property.SetValue(target, value, null);  
        }  
        catch ( Exception ex)  
        {  
          var message = string.Format("Can't set property {0} on type {1}", property.Name, type.FullName);  
          throw new InvalidOperationException(message, ex);  
        }  
      }  
     }  
   }  
 }  

Now all the magic has gone without a trace. We simply iterate through properties of Action Filter and try to resolve them using Castle Windsor kernel. Of course this dependencies should be registered before. Also we need to setup WindsorActionInvoker as default ActionInvoker in our Web Application:
 var container = new WindsorContainer();  
 DependencyResolver.SetResolver(new WindsorDependencyResolver(container.Kernel));  
 container.Register(Component.For<ISiteUser>().ImplementedBy<SiteUser>());  
 container.Register(Component.For<IActionInvoker>().ImplementedBy<WindsorActionInvoker>().DependsOn(Property.ForKey( "kernel").Eq(container.Kernel)).LifeStyle.Transient);  

One more important point here is to setup custom WindsorDependencyResolver. This class will be used to resolve dependencies instead of default MVC dependency resolver(which actually just uses Activator.CreateInstance method). Our resolver looks like this:
 public class WindsorDependencyResolver : System.Web.Mvc.IDependencyResolver  
 {  
   private readonly IKernel _kernel;  
   public WindsorDependencyResolver(IKernel kernel)  
   {  
     _kernel = kernel;  
   }  
   public object GetService(Type serviceType)  
   {  
    return _kernel.HasComponent(serviceType) ? _kernel.Resolve(serviceType) : null ;  
   }  
   public IEnumerable<object > GetServices(Type serviceType)  
   {  
     return _kernel.HasComponent(serviceType) ? _kernel.ResolveAll(serviceType).Cast<object >() : new object [] { };  
   }  
 }  

Tuesday, October 16, 2012

Testing ASP.NET MVC routes using NUnit and FakeItEasy

Let's say we have registered routes in our ASP.NET MVC application:
 routes.MapTrailingSlashRoute(  
   "Topic", // Route name  
   "news/{url}", // URL with parameters  
    new { controller = "Topic", action = "Index" } // Parameter defaults  
 );  
 
 routes.MapTrailingSlashRoute(  
   "Default", // Route name  
   "{controller}/{action}/{id}", // URL with parameters  
   new { controller = "Home", action = "Index" , id = "" } // Parameter defaults  
 );  

How can we test this routes using NUnit testing framework and FakeItEasy mocking framework?

Here's an example of how this can be done:
     [TestFixture]  
     [Category("Unit")]  
     public class when_using_registered_routes  
     {                    
       [TestFixtureSetUp]  
       public void Prepare()  
       {  
         MvcApplication.RegisterRoutes(RouteTable .Routes);   
       }  
       [Test]  
       [TestCase("~/", "Home", "Index" )]        
       [TestCase( "~/news/testing-routes-in-mvc/", "Topic" , "Index" )]  
       [TestCase("~/discussion/all/", "Discussion", "All")]        
       [TestCase( "~/discussion/widget/", "Discussion" , "Widget" )]  
       public void should_return_expected_controller_and_action(string path, string expectedController, string expectedAction)  
       {                  
         var httpContext = A.Fake<HttpContextBase>();          
         A.CallTo(() => httpContext.Request.AppRelativeCurrentExecutionFilePath).Returns(path);                  
         var routeData = RouteTable.Routes.GetRouteData(httpContext);  
         Assert.That(routeData.Values["controller"].ToString().ToLower(), Is.EqualTo(expectedController.ToLower()));  
         Assert.That(routeData.Values["action"].ToString().ToLower(), Is.EqualTo(expectedAction.ToLower()));  
       }       
     }  

Monday, October 15, 2012

How to remove component in Castle Windsor 3.0?

Say we have registered component in Castle Windsor:
 Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsGoodResult>());  

Sometimes in Unit tests i need to remove registered component from Castle Windsor and to register new one. Prior to 3.0 version we can do this:
 Container.Kernel.RemoveComponent(typeof(TestRegistratorWhichReturnsGoodResult).FullName);  
 Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsBadResult>());  

Аfter version 3.0 breaking changes we can't do this, because there is no RemoveComponent method in version 3.0.
But we can use the following approach:
  class RegistrationHandlerSelector :IHandlerSelector  
       {  
         public static bool UseBadResultRegistrator { private get; set; }  
         public bool HasOpinionAbout(string key, Type service)  
         {  
           return service == typeof(IRegistrator);            
         }  
         public IHandler SelectHandler(string key, Type service, IHandler[] handlers)  
         {  
           return handlers.First(x => UseBadResultRegistrator ? x.ComponentModel.Implementation == typeof (TestRegistratorWhichReturnsBadResult ) :  
                                      x.ComponentModel.Implementation == typeof (TestRegistratorWhichReturnsGoodResult ));                      
         }  
       }  

 Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsGoodResult>());  
 Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsBadResult>());  
 Container.Kernel.AddHandlerSelector(new RegistrationHandlerSelector());  

Now we can use RegistrationHandlerSelector.UseBadResultRegistrator field to choose which component to use.

MVC: organize your models properly

ASP.NET MVC is an excellent framework for developing scalable, testable and flexible Web applications. But bad approach can lead to bloated Controllers, poorly organized Views and chaos in Models. This article focuses on the approach for organization of models that is used successfully in several large web projects.
Consider, for example, a page which contains a large amount of information generated on server. This information may come from various sources. Following MVC paradigm we have to prepare a model that contains all necessary information and bind it to the appropriate view for display.

Consider HomeController:
 public class HomeController : BaseController  
   {  
     private readonly HomeIndexViewDataBuilder _homeIndexViewDataBuilder;  
     private readonly HomeInfoViewDataBuilder _homeInfoViewDataBuilder;  
     public HomeController(Context context, HomeIndexViewDataBuilder homeIndexViewDataBuilder, HomeInfoViewDataBuilder homeInfoViewDataBuilder)  
       : base(context)  
     {  
       _homeIndexViewDataBuilder = homeIndexViewDataBuilder;  
       _homeInfoViewDataBuilder = homeInfoViewDataBuilder;  
     }     
     public ActionResult Index()  
     {        
       var model = _homeIndexViewDataBuilder.Build();        
       return View(model);      
     }  
     public ActionResult Info(string id)  
     {  
       var model = _homeInfoViewDataBuilder.Build(id);  
       if (model!= null)  
        return View(model);  
       return TransferToNotFound();  
     }            
   }  

In order not to bloat controllers, preparation of model with necessary information takes place in Builder. In our convention Builder is a class that corresponds 1:1 to Controller-Action method and contains a single method Build. We use the following naming convention: if controller is named HomeController and Action-method is named Index then correspond Builder, will be called HomeIndexViewDataBuilder. It may look like this:
 public class HomeIndexViewDataBuilder : BaseModelBuilder  
   {  
    private readonly TopicQuery _topicQuery;  
     public HomeIndexViewDataBuilder(IModelCacher modelCacher, TopicQuery topicQuery): base(modelCacher)  
     {  
       _topicQuery = topicQuery;  
     }  
     public HomeIndexViewData Build()  
     {  
       var homeIndexViewData = new HomeIndexViewData()  
       /*filling model(fetching data from database, cache or other builders)*/  
       return homeIndexViewData;  
     }  
   }  

You can injects any objects in your Builder. In this example, objects of the following classes are injected: ModelCacher is a class for caching models, TopicQuery is a class used to fetch news from database. In some cases parameters to Build method can be passed (eg, news ID or something else). Build method returns a model that contains all necessary information to be displayed on the home page. This model does not contain any logic, only flat data for display. It may look like this:
 public class HomeIndexViewData  
   {  
     public IDictionary<string , NewsTeaserViewModel []> Teasers { get; set; }      
     public NewsTeaserViewModel[] Top { get ; set ; }      
     public NewsTeaserViewModel[] EditorsChoice { get ; set ; }      
     public NewsTeaserViewModel[] MostViewed { get ; set ; }      
     public NewsTeaserViewModel[] MostCommented { get ; set ; }      
     public NewsGalleryTeaserViewModel[] LastGalleries { get ; set ; }  
   }  

Class name follows our naming convention. That is, if Builder is called HomeIndexViewDataBuilder, then model will be HomeIndexViewData. When we say ViewData we mean a composite model. This composite model consists of variety of models, such as: NewsTeaserViewModel and NewsGalleryTeasersViewModel. By ViewModel we mean a simple model. For example, NewsTeaserViewModel may look like:
 public class NewsTeaserViewModel  
   {  
     public Guid Id { get; set; }  
     public string Url { get; set; }  
     public string Title { get; set; }  
     public string Description { get; set; }  
     public Guid? Image { get; set; }  
     public string Rubric { get; set; }  
     public DateTime? PublicationDate { get; set; }  
     public uint Comments { get; set; }  
  }  

Differences between ViewData and ViewModel:


ViewData - is a composite model returned by Builder. Builder corresponds 1:1 to a Controller-Action method. For example: HomeIndexViewDataBuilder returns HomeIndexViewData, HomeInfoViewDataBuilder returns HomeInfoViewData etc. These models are used only in correspond Controller-Action methods and nowhere else.

ViewModel - is a simple model, which is a part of any composite ViewData models. One ViewModel can be included in various ViewData models. For example, NewsTeaserViewModel can be used on the home page(in HomeIndexViewData) or on the game page(in GameIndexViewDataBuilder) or somewhere else.

Friday, October 12, 2012

Transferring to error pages from Application_Error handler in MVC3

Our task is to implement transferring(not redirection) to correspond error page when error occurs in ASP.NET MVC3 application.

Good solution for redirection to error pages is to use <customErrors> section in web.config. But we need transferring for SEO purposes.

So it was obvious to use httpErrors section instead of customErrors section:
  <httpErrors errorMode="Custom" existingResponse =" Replace" >  
     < clear />  
     < error statusCode =" 403" path= "/Error/Forbidden " responseMode="ExecuteURL" />  
     < error statusCode =" 404" path= "/Error/NotFound " responseMode="ExecuteURL" />  
     < error statusCode =" 500" path= "/Error/InternalError " responseMode="ExecuteURL" />  
   </ httpErrors>  

It's good solution, but it works only when your application is under website root. If your application is under virtual directory transferring will be broken.

The only solution i found is to use Application_Error handler in Global.asax instead of config:
 protected void Application_Error(object sender, EventArgs e)  
     {        
       var exception = Server.GetLastError();  
       if (Context.Request.IsLocal)  
         return;  
       var actionName = "InternalError";  
       if (exception is HttpException)  
       {  
         var code = (exception as HttpException).GetHttpCode();  
         switch (code)  
         {  
           case 400:  
             actionName = "NotFound";  
             break;  
           case 403:  
             actionName = "Forbidden";  
             break;  
           case 404:  
             actionName = "NotFound";  
             break;  
         }  
       }        
       var path = string.Format( "~/Error/{0}/" , actionName);        
       Server.TransferRequest(path, false);        
     }  

Code is obvious and it works fine almost for all cases. ALMOST, but not for all.

I am sure you know popular ASP.NET exception "A potentially dangerous Request.Form value was detected from the client". This exception happens when you(or ASP.NET) try to extract QueryString values, but there is "potential dangerous" in values: html tags, for example.

So, exception happens, then our Application_Error handler fires. Then we use Server.TransferRequest which PRESERVE QueryString of current request despite on second "false" parameter. Now we have "potential dangerous" again in our QueryString during this transferring, which wll trigger Application_Error again, and so on.  What we get here is cyclical transferring that can kill Application Pool easily.

To force Server.TransferRequest NOT to save QueryString of current request you have to specify QueryString explicitly in your transferring URL. For example:
 var path = string.Format( "~/Error/{0}/?transfer=1" , actionName);