Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4castle-windsor

CastleWindsor not working with route in asp.net mvc


I have a WebApplication in ASP.NET MVC using the dependency injection with CastleWindsor but when I add a route attribute, the application returns following error "The controller no found".

My ControllerInstaller

public class ControllerInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromThisAssembly()
                            .BasedOn<IController>()
                            .LifestyleTransient());
    }
}

I have a following controller:

[Route("espetaculos")]
[FrontAuthorize]
public class EventController : Controller
{
    #region DependencyInjection

    private readonly IApplication applicationWeb;
    private readonly IEventBusiness eventBusiness;
    private readonly IShowBusiness showBusiness;
    private readonly ISession sessionWeb;
    private readonly ILog iLog;

    public EventController(IEventBusiness eventBusiness, IShowBusiness showBusiness, IApplication applicationWeb, ISession sessionWeb, ILog iLog)
    {
        this.eventBusiness = eventBusiness;
        this.showBusiness = showBusiness;
        this.applicationWeb = applicationWeb;
        this.sessionWeb = sessionWeb;
        this.iLog = iLog;
    }

when I access the route "/espetaculos" here is the error

error

The Castle expects only the full path of controller?

Edit My RouteConfig class

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        //routes.MapMvcAttributeRoutes();

        //AreaRegistration.RegisterAllAreas();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Plateia.Controllers" }
        );
    }
}

Solution

  • As I suspected, you apparently haven't enabled attribute routing in MVC. Without doing so, adding the [Route] attribute on your controllers and actions will have no effect.

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
    
            // This line is required in order to scan
            // for [Route] attribute in your controllers
            routes.MapMvcAttributeRoutes();
    
            //AreaRegistration.RegisterAllAreas();
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "Plateia.Controllers" }
            );
        }
    }