Asp.Net MVC 注入和区域

发布于 2024-09-27 22:02:44 字数 1083 浏览 11 评论 0原文

我有一个使用 Ninject 进行依赖注入的站点,并且我在 Bootstrapper 类中定义了路由,如下所示:

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

        Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        Routes.MapRoute(
            null,
            "{pageTitle}",
            new { controller = "Home", action = "Page", pageTitle = "Index" }
        );

        Routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

我已向项目添加了一个区域,但默认的 AdminAreaRegistration 未注册根

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

在哪里或如何使用 Ninject 注册该区域?

I have a site that uses Ninject for dependency injection and I have Routing defined within a Bootstrapper class like so:

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

        Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        Routes.MapRoute(
            null,
            "{pageTitle}",
            new { controller = "Home", action = "Page", pageTitle = "Index" }
        );

        Routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

I have added an Area to the project but the default AdminAreaRegistration is not registering the root

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Where or how do I register the Area with Ninject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺孤伤于心 2024-10-04 22:02:44

带有源代码的示例项目现在有一个区域。看看它。 https://github.com/ninject/ninject.web.mvc/zipball/master

The sample project comming with the source code has now an area. Take a look at it. https://github.com/ninject/ninject.web.mvc/zipball/master

通知家属抬走 2024-10-04 22:02:44

您正在调用 RegisterAllAreas() 吗?

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
}

请注意,它必须在 RegisterRoutes() 之前调用。

are you calling RegisterAllAreas()?

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
}

note it must be called before RegisterRoutes().

惜醉颜 2024-10-04 22:02:44

你解决这个问题了吗?

我遇到的问题是我的 NinjectControllerFactory 无法解析引用区域中定义的控制器的 url。我收到以下消息:

IControllerFactory“myWebSite.WebUI.Infrastruct.NinjectControllerFactory”未返回名称“admin”的控制器。

如果我将控制器移动到根 Controllers 文件夹,它将解析 url。

Have you resolved this issue?

I have the problem where my NinjectControllerFactory is not resolving urls that reference controllers defined in areas. I get the following message:

The IControllerFactory 'myWebSite.WebUI.Infrastructure.NinjectControllerFactory' did not return a controller for the name 'admin'.

If I move the controller to the root Controllers folder, it will resolve the url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文