Umbraco.GodMode icon indicating copy to clipboard operation
Umbraco.GodMode copied to clipboard

System.InvalidCastException: Unable to cast object of type 'System.Web.Mvc.Routing.RouteCollectionRoute' to type 'System.Web.Routing.Route'.

Open mistyn8 opened this issue 3 years ago • 7 comments

Seeing a route enumeration error in diagnostics, any ideas? Umbraco 8.17.1 God Mode 2.4.1

Might be associated with Swashbuckle 5.6.0 (SwaggerApi) ?

Exception
System.InvalidCastException: Unable to cast object of type 'System.Web.Mvc.Routing.RouteCollectionRoute' to type 'System.Web.Routing.Route'.
   at Diplo.GodMode.Services.DiagnosticService.<>c.<GetDiagnosticGroups>b__6_4(RouteBase r)
   at System.Linq.Enumerable.<>c__DisplayClass7_0`3.<CombineSelectors>b__0(TSource x)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at Diplo.GodMode.Services.DiagnosticService.GetDiagnosticGroups()
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---

mistyn8 avatar Mar 28 '22 20:03 mistyn8

Could it be section.Diagnostics.AddRange(RouteTable.Routes.Select(r => (Route)r).Select(r => new Diagnostic(r.RouteHandler.GetType().Name, r.Url)));

might need to be.. section.Diagnostics.AddRange(RouteTable.Routes.OfType<Route>().Select(r => new Diagnostic(r.RouteHandler.GetType().Name, r.Url)));

Dropping webApi routes?

mistyn8 avatar Mar 28 '22 20:03 mistyn8

Also having issues using RouteTable.Routes.MapMvcAttributeRoutes(); and

using Microsoft.Owin.Security;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using Umbraco.Core.Logging;

namespace LP.Web.Controllers
{
    [RoutePrefix("Account")]
    public class AccountSurfaceController : PluginController
    {
        [Route("LogIn")]
        public ActionResult LogIn(string returnUrl)
        {
            // Use the default policy to process the sign up / sign in flow
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties {
                //RedirectUri = "/" 
                RedirectUri = returnUrl ?? "/"
            });

            return new HttpUnauthorizedResult();
        }
     }
}

Means r.RouteHandler is null ??

mistyn8 avatar Mar 28 '22 21:03 mistyn8

image

mistyn8 avatar Mar 28 '22 22:03 mistyn8

It would appear that these end up as LinkGenerationRoute and as such don't have RouteHandlers?

mistyn8 avatar Mar 29 '22 10:03 mistyn8

Hi! Thanks for reporting this. It's not one I've seen before or had reported before. Is it a standard Umbraco site or are you doing anything "custom" with routing?

I think with something like this, that I can't replicate, then would you be able to find a fix yourself? Doesn't need to be a PR, just the line of code. I'm guessing some kind of null check would be needed?

DanDiplo avatar Mar 30 '22 08:03 DanDiplo

Sorry been a while.. Had it crop up again on an old site.. so implemented a fix

section = new DiagnosticSection("MVC Routes");
 section.Diagnostics.AddRange(RouteTable.Routes.OfType<Route>().Select(r => new Diagnostic(r?.RouteHandler?.GetType()?.Name ?? "n/a", r?.Url ?? string.Empty)));
 sections.Add(section);

results in this for those System.Web.Mvc implementations.. image

namespaceCode.Controllers.Surface
{
	/// <summary>
	/// Summary description for DevelopmentInfoController
	/// </summary>
	[RoutePrefix("api/devinfo")]
    public class DevelopmentInfoController : RenderMvcController
    {
        public DevelopmentInfoController()
        {
            
        }

		[HttpGet]
		[Route("getdevbox/{id}")]
		public ActionResult GetDevelopmentInfo(int id)
		{
			var node = Umbraco.Content(id);
			
			if (node != null)
			{
				return PartialView("~/Views/Partials/Content/DevelopmentBox.cshtml", node);
			}
			else
			{
				return null;
			}
		}
	}
}

mistyn8 avatar Sep 05 '23 15:09 mistyn8

Cheers! Will look at committing this to the main source code next time I'm working on it. Thanks for helping.

DanDiplo avatar Sep 05 '23 16:09 DanDiplo