简体   繁体   English

CastleWindsor无法在ASP.NET MVC中使用路由

[英]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". 我在ASP.NET MVC中有一个WebApplication,使用的是CastleWindsor依赖注入,但是当我添加route属性时,应用程序返回以下错误“找不到控制器”。

My ControllerInstaller 我的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 当我访问路线“ / espetaculos”时,这是错误

错误

The Castle expects only the full path of controller? 城堡只期望控制器的完整路径吗?

Edit My RouteConfig class 编辑我的RouteConfig类

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

As I suspected, you apparently haven't enabled attribute routing in MVC. 正如我所怀疑的那样,您显然没有在MVC中启用属性路由。 Without doing so, adding the [Route] attribute on your controllers and actions will have no effect. 否则,在控制器和操作上添加[Route]属性将无效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM