2

Getting an error in my (at this stage) simple c# MVC3 (Razor) application, and I can't seem to find out what is causing the error below.

[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.Last(IEnumerable`1 source) +4188046
   RouteDebug.RouteDebuggerHttpModule.OnBeginRequest(Object sender, EventArgs e) +78
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

this is all I get, no line numbers, no nothing.

I've tried putting breakpoints in various locations in the code, including the application_start sectin of Global.asax.cs, but these never get hit, the program just immediately bombs out with the stack trace as soon as i start debugging.

The structure of the application is as follows:

[Application.Core]
+Properties
+Reference
+Controllers
+Helpers
+Models
+AdminAreaRegistration.cs
+Global.asax.cs
[Application.Tests]
[Application.UI]
+Properties
+References
+App_Data
+Areas
    +Admin
        +Views
+Assets (css/js/images)
+Views

I've tried a few suggestions already, including adding the Application.Core namespace to the AdminAreaRegistration.cs file, and removing obj/bin and rebuilding.

I just can't see whats causing the problem.

Update

AdminAreaRegistration.cs

using System.Web.Mvc;

namespace Application.Core
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new {controller = "Page", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Application.Core
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Page", action = "Homepage", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}
kolin
  • 2,326
  • 1
  • 28
  • 46
  • 1
    do you have "glimpse" or [RouteDebugger](http://nuget.org/packages/routedebugger) or anything similar installed? `RouteDebuggerHttpModule` sounds very suspect here... – Marc Gravell Sep 13 '12 at 08:31
  • I have Phil Haack's routedebugger (2.1.4.0) installed when i first started the app. It has been working, but since (I think) I created an area and moved the views into this area I've been getting this error. But i couldn#'t tell you exactly when it started. – kolin Sep 13 '12 at 08:34
  • Looks like it is something to do with the Admin area that I have set up, I'm going to have to look into this later, as other projects call. – kolin Sep 24 '12 at 11:10

2 Answers2

2

Glancing at RouteDebugger in reflector, the offending code is:

private static void OnBeginRequest(object sender, EventArgs e)
{
    if (RouteTable.Routes.Last<RouteBase>() != DebugRoute.Singleton)
    {
        RouteTable.Routes.Add(DebugRoute.Singleton);
    }
}

So: it sounds like somehow you have no routes. However, I would also wonder whether this module should actually use LastOrDefault, or basically cope better with this scenario.

Have you ensured that you have the current and up-to-date version of RouteDebugger ?

As for why you have no routes: maybe the pipeline is running in a different order now? First thing to do: remove that module and see if everything else works. If the site works fine minus that module, then ask Phil about it.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I've added the Routes for Admin and Global into the original question just to see if there is anything glaringly obvious that i've missed – kolin Sep 13 '12 at 09:05
  • I removed the RouteDebugger from the solution using nuget, and I get a 404 which implies to me that the routes aren't either working or not registered... would this be correct? – kolin Sep 13 '12 at 10:09
  • @kolin yes, fix that *first*, then put `RouteDebugger` back in. Sounds like you deleted the line in global.asax that configures the routing table. – Marc Gravell Sep 13 '12 at 11:15
0

It's caused my calling the extension method Last() on a collection that has no elements, or calling Last(expression) on a collection that has no matching elements.

I would do a search through my code to try and identify where you have used this kind of calls. (just do a search in entire solution)

My best guess is that you have put this logic directly into one of your views, and not in the controller.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • There is no logic hardcoded into the views. all logic to populate the views are produced in the controllers, and all linq queries end in FirstOrDefault – kolin Sep 13 '12 at 08:41