MVC example problem - null reference exception

Topics: Developer Forum
Feb 10, 2009 at 11:40 AM
hi guys, i'm new to n2 so pls don't be hard on me :)
i've installed asp.net mvc release candidate and trying to run mvc example project, which i downloaded from codeplex.

i was following installation steps (go to localhost/edit/install), everything went fine, but when i call http://localhost/, i get NullReferenceException:

Server Error in '/' Application.

Object reference not set to an instance of an object.

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.NullReferenceException: Object reference not set to an instance of an object.

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:

[NullReferenceException: Object reference not set to an instance of an object.]
   N2.Web.Mvc.FallbackContentController.Index() +21
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +43
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +350
   System.Web.Mvc.Controller.ExecuteCore() +110
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +119
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +41
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Additionally:
when i hit http://localhost/edit/install  i get:

Server Error in '/' Application.

A route named 'Default' is already in the route collection. Route names must be unique.
Parameter name: name

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.ArgumentException: A route named 'Default' is already in the route collection. Route names must be unique.
Parameter name: name

Source Error:

Line 13:             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Line 14:             routes.Add(new ContentRoute(engine, new MvcRouteHandler()));
Line 15:             routes.MapRoute(
Line 16:                "Default",                                              // Route name
Line 17:                "{controller}/{action}/{id}",                           // URL with parameters

Source File: D:\Slafco\PrivateDev\DevelopWeb\n2\mvc\wwwroot\Global.asax.cs    Line: 15

Stack Trace:

[ArgumentException: A route named 'Default' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.Routing.RouteCollection.Add(String name, RouteBase item) +16820
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints, String[] namespaces) +208
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults) +16
   MvcTest.GlobalApplication.RegisterRoutes(RouteCollection routes, IEngine engine) in D:\DevelopWeb\n2\mvc\wwwroot\Global.asax.cs:15
   MvcTest.GlobalApplication.Init() in D:\DevelopWeb\n2\mvc\wwwroot\Global.asax.cs:25
   System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +335

Using IIS 6.0 on Win2003. ASP.NET MVC RC.
global.asax.cs was not changed.

Coordinator
Feb 10, 2009 at 7:12 PM
Any chance you could get the latest from the repository and see if that solves this problem? I recall seeing it but I can't reproduce it since I made some changes.
Feb 10, 2009 at 7:20 PM
sure, i will try it, thx. in meanwhile i tried mvc example on vista/iis7 integrated mode but with same results.
First research looks like ContentController<T>::CurrentItem is null. Somewhere in ContentRoute the request is forwarded to controller without populating that value.
Feb 10, 2009 at 7:37 PM
i tried trunk version, it works better but still there is an issue (i use ASP.NET MVC RC). you are using which version of ASP.NET MVC ? Can you try with latest release?
this error still appears: first time you request the root page, or /edit/ page, you get exception i already posted (when you then hit F5-refresh, it's okay):

A route named 'Default' is already in the route collection. Route names must be unique.
Parameter name: name

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.ArgumentException: A route named 'Default' is already in the route collection. Route names must be unique.
Parameter name: name

Source Error:

Line 13:             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Line 14:             routes.Add(new ContentRoute(engine, new MvcRouteHandler()));
Line 15:             routes.MapRoute(
Line 16:                "Default",                                              // Route name
Line 17:                "{controller}/{action}/{id}",                           // URL with parameters

Source File: D:\Slafco\PrivateDev\DevelopWeb\n2\mvc\wwwroot\Global.asax.cs    Line: 15

Stack Trace:

[ArgumentException: A route named 'Default' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.Routing.RouteCollection.Add(String name, RouteBase item) +16820
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints, String[] namespaces) +208
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults) +16
   MvcTest.GlobalApplication.RegisterRoutes(RouteCollection routes, IEngine engine) in D:\DevelopWeb\n2\mvc\wwwroot\Global.asax.cs:15
   MvcTest.GlobalApplication.Init() in D:\DevelopWeb\n2\mvc\wwwroot\Global.asax.cs:25
   System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +335
Feb 10, 2009 at 7:43 PM
i believe i have workaround for the issue, with following fix also first request behaves correctly.

when registering routes, use approach used in examples in MSDN instead of overriding Init method
Global.asax.cs (FIXED):

 

void Application_Start(object sender, EventArgs e)

 

{

 

IEngine engine = N2.Context.Initialize(false);

 

RegisterRoutes(

RouteTable.Routes, engine);

 

}

 

 

//public override void Init()

 

 

//{

 

 

// //IEngine engine = N2.Context.Initialize(false);

 

 

// //RegisterRoutes(RouteTable.Routes, engine);

 

 

// base.Init();

 

 

//}

 

Coordinator
Feb 10, 2009 at 8:47 PM
Great, I've commited the fix.
Feb 16, 2009 at 11:14 PM
I downloaded changeset 29910..  Is the MVC example included?
Coordinator
Feb 17, 2009 at 6:25 AM
It's here http://code.google.com/p/n2cms/source/checkout