title: An error occurred when trying to create a controller of type 'TypeNewsController'
body: <p>I have searched long and hard but found nothing that helped yet. Where am I going wrong? I really do not know what to do. I wrote all the details below. I've tried and did not succeed.</p>
  
  <blockquote>
    <p>An error occurred when trying to create a controller of type
    'TypeNewsController'. Make sure that the controller has a
    parameterless public constructor.</p>
  </blockquote>
  
  <pre><code> public class MvcApplication : System.Web.HttpApplication
      {
          protected void Application_Start()
          {
              WebApiConfig.Register(GlobalConfiguration.Configuration);
              AreaRegistration.RegisterAllAreas();
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
              Bootstrapper.Run();
          }
      }
  </code></pre>
  
  <p>my apicontroller :</p>
  
  <pre><code>public class TypeNewsController : ApiController
      {
          private readonly ITypeNewsService _typeNewsService;
  
          public TypeNewsController(ITypeNewsService typeNewsService)
          {
              _typeNewsService = typeNewsService;
          }
          [HttpGet]
          public TypeNewsResponse Get([ModelBinder] PageRequest model)
          {
              model = model ?? new PageRequest();
              var output = _typeNewsService.GetTypeNewss().ToList();
              return new TypeNewsResponse
              {
                  Page = model.PageIndex,
                  Records = model.PageSize,
                  Rows = output.ToList(),
                  Total = output.Count() / model.PageSize,
              };
          }
      }
  </code></pre>
  
  <p>error :</p>
  
  <pre><code>&lt;Error&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  An error occurred when trying to create a controller of type 'TypeNewsController'. Make sure that the controller has a parameterless public constructor.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__0.MoveNext()
  &lt;/StackTrace&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  Type 'JuventusNewsSiteApk.Controllers.TypeNewsController' does not have a default constructor
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.ArgumentException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1&amp; activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  &lt;/StackTrace&gt;
  &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>Bootstrapper class :</p>
  
  <pre><code>public static class Bootstrapper
      {
          public static void Run()
          {
              SetAutofacContainer();
              //Configure AutoMapper
              AutoMapperConfiguration.Configure();
          }
  
          private static void SetAutofacContainer()
          {
              var builder = new ContainerBuilder();
              builder.RegisterControllers(Assembly.GetExecutingAssembly());
              builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
              builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().InstancePerRequest();
              builder.RegisterType&lt;DatabaseFactory&gt;().As&lt;IDatabaseFactory&gt;().InstancePerRequest();
              builder.RegisterAssemblyTypes(typeof(NewsRepository).Assembly)
                  .Where(t =&gt; t.Name.EndsWith("Repository"))
                  .AsImplementedInterfaces().InstancePerRequest();
              builder.RegisterAssemblyTypes(typeof(NewsService).Assembly)
                  .Where(t =&gt; t.Name.EndsWith("Service"))
                  .AsImplementedInterfaces().InstancePerRequest();
  
              builder.RegisterAssemblyTypes(typeof(DefaultFormsAuthentication).Assembly)
                  .Where(t =&gt; t.Name.EndsWith("Authentication"))
                  .AsImplementedInterfaces().InstancePerRequest();
  
              builder.Register(
                  c =&gt; new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new JuventusNewsApkEntities())))
                  .As&lt;UserManager&lt;ApplicationUser&gt;&gt;().InstancePerRequest();
  
              builder.RegisterFilterProvider();
              IContainer container = builder.Build();
              DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
          }
      }
  </code></pre>
  
  <p><strong>update :</strong></p>
  
  <pre><code> public class TypeNewsService : ITypeNewsService
      {
          private readonly ITypeNewsRepository _typeNewsRepository;
          private readonly IUnitOfWork _unitOfWork;
          public TypeNewsService(ITypeNewsRepository typeNewsRepository,
              IUnitOfWork unitOfWork)
          {
              _typeNewsRepository = typeNewsRepository;
              _unitOfWork = unitOfWork;
          }
  
          #region ITypeNewsService Member
  
          public void AddTypeNews(TypeNews typeNews)
          {
              _typeNewsRepository.Add(typeNews);
              SaveTypeNews();
          }
  
          public void DeleteTypeNews(int id)
          {
              _typeNewsRepository.DeleteById(id);
              SaveTypeNews();
          }
  
          public IEnumerable&lt;TypeNews&gt; GetTypeNewss()
          {
              var output = _typeNewsRepository.GetAll();
              return output;
          }
  
          public void SaveTypeNews()
          {
              _unitOfWork.Commit();
          }
  
          #endregion
  
      }
  
      public interface ITypeNewsService
      {
          void AddTypeNews(TypeNews typeNews);
          void DeleteTypeNews(int id);
          IEnumerable&lt;TypeNews&gt; GetTypeNewss();
          void SaveTypeNews();
      }
  </code></pre>