Question

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);
        }
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top