asp.net-mvc – 尝试创建类型为’TypeNewsController’的控制器时出错
发布时间:2020-12-05 04:10:23 所属栏目:asp.Net 来源:互联网
导读:我已经搜索了很长时间,但没有发现任何帮助.我哪里错了?我真的不知道该怎么办我写下了所有的细节.我试过,没有成功. An error occurred when trying to create a controller of type ‘TypeNewsController’. Make sure that the controller has a parameterle
我已经搜索了很长时间,但没有发现任何帮助.我哪里错了?我真的不知道该怎么办我写下了所有的细节.我试过,没有成功.
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(); } } 我的apicontroller: 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,}; } } 错误: <Error> <Message>An error has occurred.</Message> <ExceptionMessage> An error occurred when trying to create a controller of type 'TypeNewsController'. Make sure that the controller has a parameterless public constructor. </ExceptionMessage> <ExceptionType>System.InvalidOperationException</ExceptionType> <StackTrace> 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.<SendAsync>d__0.MoveNext() </StackTrace> <InnerException> <Message>An error has occurred.</Message> <ExceptionMessage> Type 'JuventusNewsSiteApk.Controllers.TypeNewsController' does not have a default constructor </ExceptionMessage> <ExceptionType>System.ArgumentException</ExceptionType> <StackTrace> 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& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,Type controllerType) </StackTrace> </InnerException> </Error> Bootstrapper类: 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<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest(); builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest(); builder.RegisterAssemblyTypes(typeof(NewsRepository).Assembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces().InstancePerRequest(); builder.RegisterAssemblyTypes(typeof(NewsService).Assembly) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces().InstancePerRequest(); builder.RegisterAssemblyTypes(typeof(DefaultFormsAuthentication).Assembly) .Where(t => t.Name.EndsWith("Authentication")) .AsImplementedInterfaces().InstancePerRequest(); builder.Register( c => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new JuventusNewsApkEntities()))) .As<UserManager<ApplicationUser>>().InstancePerRequest(); builder.RegisterFilterProvider(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } 更新: 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<TypeNews> 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<TypeNews> GetTypeNewss(); void SaveTypeNews(); } 解决方法您的控制器是WebApi控制器,Autofac的注册与MVC注册不同. WebApi不使用DependencyResolver,因此您需要告诉WebApi专门使用Autofac解析器.您需要将其添加到您的SetAutofacContainer代码中: // Create the depenedency resolver. var resolver = new AutofacWebApiDependencyResolver(container); // Configure Web API with the dependency resolver. GlobalConfiguration.Configuration.DependencyResolver = resolver; 有关更多信息,请参阅https://code.google.com/p/autofac/wiki/WebApiIntegration. (编辑:百色站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 登录后对Membership.GetAllUsers()的例外情况:
- asp.net-mvc – IIS显示服务器错误而不是自定义错误
- 有标签的ASP.NET WebControl吗?
- 如何在ASP.NET和C#中加载下拉列表?
- ASP.NET异步方法问题
- asp.net实现生成缩略图及给原始图加水印的方法示例
- asp.net-mvc – MVC“添加控制器”是“无法检索元数据…配置
- 如何利用ASP.net IIS 7.5中的浏览器缓存
- ASP.NET(C#)应用程序配置文件app.config/web.config的增、删
- 序列化 – Newtonsoft中的TypeNameHandling需要$type作为第
推荐文章
站长推荐
- asp.net-mvc – 你如何指定在列表框中显示多少项
- asp.net-mvc – 尝试将asp.net web发布到Azure时
- asp.net – Dropzone没有绑定到模型
- asp.net – 使用SignalR编译项目时,我必须做一个
- asp.net-mvc – 如何在RegularExpression中忽略大
- Asp.net(C#)读取数据库并生成JS文件制作首页图片
- asp.net – 在SQL Server中将日期转换为刻度
- asp.net-mvc – 在ajax请求中返回PDF
- ASP.NET web.config中数据库连接字符串connectio
- 验证 – Asp.Net MVC:子请求的执行失败.请查看I
热点阅读