Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mistyn8 opened this issue Mar 28, 2022 · 7 comments

Comments

@mistyn8
Copy link

mistyn8 commented Mar 28, 2022

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
Copy link
Author

mistyn8 commented Mar 28, 2022

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
Copy link
Author

mistyn8 commented Mar 28, 2022

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
Copy link
Author

mistyn8 commented Mar 28, 2022

image

@mistyn8
Copy link
Author

mistyn8 commented Mar 29, 2022

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

@DanDiplo
Copy link
Owner

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?

@mistyn8
Copy link
Author

mistyn8 commented Sep 5, 2023

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;
			}
		}
	}
}

@DanDiplo
Copy link
Owner

DanDiplo commented Sep 5, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants