Base solution for your next web application
Open Closed

Area controller constructor dependency injection #2015


User avatar
0
roxybox created

I have created a new area called Training within my MVC application along with a controller called courses. If I go to the controller in a browser (/Training/Courses) I get the expected response (View rendered).

If I then add my appservice to be injected in the constructor the route is no longer able to find the controller resulting in a broken URL.

This controller was from the main controllers folder and was working OK before moving the controller to the area (updated the namespace).

Do I need to do any setup to make areas work with dependency injection?


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Is your solution AspNet Core 1.x or AspNet MVC 5.x? Also, did you register your service to DI?

  • User Avatar
    0
    roxybox created

    Hi

    The app is mvc 5 and the app service worked previously when the controller was not in an area. I have created a new controller without any dependencies (in the constructor) an the controller works OK.

    Do I need to register the app service within the area as well or will the auto registration be enough (when using the interfaces in the docs)?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your controller definition. Normally all controllers must be registered to dependency injection if they implement AbpController.

  • User Avatar
    0
    roxybox created

    Courses controller in the Training area

    using System.Collections.Generic;
    using System.Web.Mvc;
    using Abp.Web.Mvc.Authorization;
    using Roxybox.TrainingManager.Authorization;
    using Roxybox.TrainingManager.Training;
    using Roxybox.TrainingManager.Training.Dto;
    using Roxybox.TrainingManager.Web.Controllers;
    using Roxybox.TrainingManager.Web.Models;
    using Roxybox.TrainingManager.Web.Models.Training;
    
    namespace Roxybox.TrainingManager.Web.Areas.Training.Controllers
    {
    
        [AbpMvcAuthorize(PermissionNames.Pages_Training_Courses)]
        public class CoursesController : TrainingManagerControllerBase
        {
            private readonly ICourseAppService _courseAppService;
    
            public CoursesController(ICourseAppService courseAppService)
            {
                _courseAppService = courseAppService;
            }
    
            public ActionResult Index()
            {
                //Run SUT
                var output = _courseAppService.GetCourses(new GetCoursesInput());
    
                var model = new CoursesViewModel
                {
                    Courses = output.Courses,
                    PageHeader = new PageHeaderViewModel("Training", "Courses", new List<BreadcrumbViewModel>()
                    {
                        new BreadcrumbViewModel("Dashboard","/"),
                        new BreadcrumbViewModel("Training","/Training"),
                        new BreadcrumbViewModel("Courses")
                    })
                 };
                
                return View(model);
            }
        }
    }
    

    Base controller inherits from AbpController

    public abstract class TrainingManagerControllerBase : AbpController