Ninject 2 和 MVC 2.0
我已将项目从 VS2008 和 MVC1 更新到 VS2010 和 MVC2。我遇到 Ninject 无法在区域内找到控制器的问题
这是我的 global.asax.cs 文件:
namespace Website
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : NinjectHttpApplication
{
public static StandardKernel NinjectKernel;
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Balance",
"Balance/{action}/{month}/{year}",
new { controller = "Balance", action = "Index", month = DateTime.Now.Month, year = DateTime.Now.Year }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Login", action = "Index", id = "" } // Parameter defaults
);
}
/*
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
// initializes the NHProfiler so you can see what is going on with your queries
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
}
*/
protected override void OnApplicationStarted()
{
RegisterRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
protected void Application_Error(object sender, EventArgs e)
{
var errorService = NinjectKernel.Get<IErrorLogService>();
errorService.LogError(HttpContext.Current.Server.GetLastError().GetBaseException(), "AppSite");
}
protected override IKernel CreateKernel()
{
if (NinjectKernel == null)
{
NinjectKernel = new StandardKernel(new ServiceModule());
}
return NinjectKernel;
}
}
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IHelper>().To<Helper>().InRequestScope();
Bind<IErrorLogService>().To<ErrorLogService>();
Bind<INHSessionFactory>().To<NHSessionFactory>().InSingletonScope();
Bind<ISessionFactory>().ToMethod(ctx =>
ctx.Kernel.Get<INHSessionFactory>().GetSessionFactory())
.InSingletonScope();
Bind<INHSession>().To<NHSession>();
Bind<ISession>().ToMethod(ctx => ctx.Kernel.Get<INHSession>().GetSession());
}
}
}
访问 /Controllers 文件夹内的控制器工作正常,但访问 /Areas/Member/Controller 内的控制器会引发以下错误:
Server Error in '/' Application.
Cannot be null
Parameter name: service
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Cannot be null
Parameter name: service
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentNullException: Cannot be null
Parameter name: service]
Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) +193
Ninject.Web.Mvc.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +41
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +66
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +124
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8771488
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Version Information: Microsoft .NET Framework Version:4.0.30128; ASP.NET Version:4.0.30128.1
Url对于此请求是 /Member/Controller/,如果我也更改 Url /Controller 控制器会触发,但我收到错误消息,系统无法在路径 /Views 中找到视图,
而它应该在 /Area/Members/Views 中查找
我要么在升级过程中做错了什么,要么错过了一些东西,但我只是不知道是什么。我已经尝试解决这个问题三天了...
I've updated a project to VS2010 and MVC2 from VS2008 and MVC1. I'm having problems with Ninject not finding controllers within Areas
Here is my global.asax.cs file:
namespace Website
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : NinjectHttpApplication
{
public static StandardKernel NinjectKernel;
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Balance",
"Balance/{action}/{month}/{year}",
new { controller = "Balance", action = "Index", month = DateTime.Now.Month, year = DateTime.Now.Year }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Login", action = "Index", id = "" } // Parameter defaults
);
}
/*
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
// initializes the NHProfiler so you can see what is going on with your queries
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
}
*/
protected override void OnApplicationStarted()
{
RegisterRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
protected void Application_Error(object sender, EventArgs e)
{
var errorService = NinjectKernel.Get<IErrorLogService>();
errorService.LogError(HttpContext.Current.Server.GetLastError().GetBaseException(), "AppSite");
}
protected override IKernel CreateKernel()
{
if (NinjectKernel == null)
{
NinjectKernel = new StandardKernel(new ServiceModule());
}
return NinjectKernel;
}
}
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IHelper>().To<Helper>().InRequestScope();
Bind<IErrorLogService>().To<ErrorLogService>();
Bind<INHSessionFactory>().To<NHSessionFactory>().InSingletonScope();
Bind<ISessionFactory>().ToMethod(ctx =>
ctx.Kernel.Get<INHSessionFactory>().GetSessionFactory())
.InSingletonScope();
Bind<INHSession>().To<NHSession>();
Bind<ISession>().ToMethod(ctx => ctx.Kernel.Get<INHSession>().GetSession());
}
}
}
Accessing controllers within the /Controllers folder works OK, but accessing controllers within a /Areas/Member/Controller throws the following error:
Server Error in '/' Application.
Cannot be null
Parameter name: service
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Cannot be null
Parameter name: service
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentNullException: Cannot be null
Parameter name: service]
Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) +193
Ninject.Web.Mvc.NinjectControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +41
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +66
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +124
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8771488
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Version Information: Microsoft .NET Framework Version:4.0.30128; ASP.NET Version:4.0.30128.1
The Url for this request is /Member/Controller/, If I change the Url too /Controller the controller fires but I get an error that the system cannot find the View in the path /Views
When it should be looking in /Area/Members/Views
I have either done something wrong in the upgrade or I'm missing something bt I just can't figure out what. I've been trying to figure this out for 3 days...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从核心更新,我刚刚提交了修复: http://github.com/ninject/ninject .web.mvc
-伊恩
Update from the core, I just committed a fix: http://github.com/ninject/ninject.web.mvc
-Ian