asp.net-mvc – ASP.NET MVC WebApi:没有为此对象定义的无参数
我有一个ASP.NET MVC 4应用程序,我想实现工作单元模式.
在我的Web项目中,我有: IocConfig.cs using System.Web.Http; using NinjectMVC.Data; using NinjectMVC.Data.Contracts; using Ninject; namespace NinjectMVC { public class IocConfig { public static void RegisterIoc(HttpConfiguration config) { var kernel = new StandardKernel(); // Ninject IoC // These registrations are "per instance request". // See http://blog.bobcravens.com/2010/03/ninject-life-cycle-management-or-scoping/ kernel.Bind<RepositoryFactories>().To<RepositoryFactories>() .InSingletonScope(); kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>(); kernel.Bind<INinjectMVCUow>().To<NinjectMVCUow>(); // Tell WebApi how to use our Ninject IoC config.DependencyResolver = new NinjectDependencyResolver(kernel); } } } Global.asax中 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace NinjectMVC { // Note: For instructions on enabling IIS6 or IIS7 classic mode,// visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // Tell WebApi to use our custom Ioc (Ninject) IocConfig.RegisterIoc(GlobalConfiguration.Configuration); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); } } } PersonsController.cs using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using NinjectMVC.Data.Contracts; using NinjectMVC.Model; namespace NinjectMVC.Controllers { public class PersonsController : ApiControllerBase { public PersonsController(INinjectMVCUow uow) { Uow = uow; } #region OData Future: IQueryable<T> //[Queryable] // public IQueryable<Person> Get() #endregion // GET /api/persons public IEnumerable<Person> Get() { return Uow.Persons.GetAll() .OrderBy(p => p.FirstName); } // GET /api/persons/5 public Person Get(int id) { var person = Uow.Persons.GetById(id); if (person != null) return person; throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } // OData: GET /api/persons/?firstname='Hans'' // With OData query syntax we would not need such methods // /api/persons/getbyfirstname?value=Joe1 [ActionName("getbyfirstname")] public Person GetByFirstName(string value) { var person = Uow.Persons.GetAll() .FirstOrDefault(p => p.FirstName.StartsWith(value)); if (person != null) return person; throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } // Update an existing person // PUT /api/persons/ public HttpResponseMessage Put(Person person) { Uow.Persons.Update(person); Uow.Commit(); return new HttpResponseMessage(HttpStatusCode.NoContent); } } } 当我尝试冲浪时:http://www.domain.com/Persons/Get我得到: 没有为此对象定义无参数构造函数. 有没有我错过的东西?我将不胜感激任何帮助. 这是项目的zip文件,以便更好地参考: http://filebin.ca/E6aoOkaUpbQ/NinjectMVC.zip 解决方法
Wep.API使用与MVC框架不同的IDependencyResolver.
当您使用HttpConfiguration.DependencyResolver时,它仅适用于ApiControllers.但你的ApiControllerBase派生自Controller … 所以你的ApiControllerBase应该继承自ApiController. 在ApiBaseController.cs中更改它: public abstract class ApiControllerBase : ApiController { } 如果要将依赖项注入常规Controller派生类,则需要使用(在IocConfig中): System.Web.Mvc.DependencyResolver.SetResolver(new NinjectMvcDependencyResolver(container)); 请注意,在这种情况下,您无法使用NinjectDependencyResolver,因为它适用于ApiControllers. 所以你需要一个不同的NinjectMvcDependencyResolver,它应该实现System.Web.Mvc.IDependencyResolver. public class NinjectMvcDependencyResolver: NinjectDependencyScope,System.Web.Mvc.IDependencyResolver { private IKernel kernel; public NinjectDependencyResolver(IKernel kernel) : base(kernel) { this.kernel = kernel; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- .net – RegularExpressionValidator使用除R??egex之外的其
- ASP.NET -- WebForm -- Cookie的使用
- asp.net-mvc-3 – Asp.Net MVC 3 – @ Html.Action不会呈现
- ASP.NET MVC教程一:ASP.NET MVC简介
- asp.net-mvc – Olark免费替代品
- asp.net-mvc – [HttpPost] public ActionResult Create(Fo
- 如果method参数是string或int,则ASP.NET WebAPI抛出404
- asp.net-mvc – 统一MVC和Web Api – 视图和json的相同控制
- [你必须知道的.NET] 第三回:历史纠葛:特性和属性
- asp.net – WinForms和Asp的类库中的异常处理
- 我应该在ASP.NET MVC中构建我的下一个Web应用程序
- asp.net-mvc – @ Html.DropDownListFor如何添加
- asp.net-mvc – 地址字段的自定义ASP.NET MVC验证
- 配置 – ASP.NET Web Api:项目需要SQL Server E
- asp.net – 如何在客户端验证失败后保持滚动位置
- asp.net – 哪个控件导致回发?
- asp.net – 如何在Kendo UI中获取下拉菜单的选定
- asp.net-mvc – Visual Studio 2010 Full和ASP.N
- asp.net-ajax – ScriptManager.RegisterClientS
- asp.net-mvc – ASP.Net MVC的LoginStatus?