cloudscribe/cloudscribe.Web.Navigation

AttributeRouting with missing controller method, but with entry in navigation.xml throws exception

Closed this issue · 5 comments

Environment:
Asp.Net Core 2.1, cloudscribe.Web.Navigation 2.1.1, Attribute Routing without MVC default route.

I have an entry in navigation.xml (using controller="abc" and action="xyz") for which I do not have an implementation (yet), and another for which I have a RouteConstraint.
Building the navigation throws a NullReferenceException when calling Model.GetClass(node.Value, "nav-item", "active", true) in Views\Shared\NavigationNodeChildDropdownPartial.cshtml. I would expect a navigation item without a link. Then I could decide not to show the item at all with some additional code in the view.

When I add app.UseMvcWithDefaultRoute() besides attribute routing the navigation shows as expected, but the navigation items in question with the (wrong) link to the missing implementation. The same applies in case of a RouteConstraint while the preserved route parameter is missing. So app.UseMvcWithDefaultRoute() is not a workaround.

Is this by design and/or how could the exception be avoided/caught?

you didn't post enough detail to see exactly what is causing the error and where the error is happening.

In general I would say yes it is expected that garbage in leads to errors and garbage out, you should not configure invalid nodes in the xml and expect them to not throw errors, you should wait to add them to the xml until after you implement them.

The extension method Model.GetClass has this for its first line of code so if you pass in a null node it returns the passed in inputClass

if (node == null) return inputClass;

So it is already doing a null check, and there are other null guard checks in the method.

You can always override the views by putting them in your own project (local views will be used first) and if you want to wrap things in try catch to swallow exceptions you can do that. I think it is better if there are invalid nodes that naturally throw exceptions then it leads you to fix the problem.

You can also propose changes in the code and give reasons why you propose the change if you think there is a bug or that it should behave differently. At the moment I'm not seeing any problem with the way it works.

Very strange:

  • When adding the new nuget package v2.1.2, I get the same exception again (Model.GetClass() throws)
  • When referencing the latest v2.1.2 local repo clone it works.

I think it was a packaging error, maybe that fix did not get into 2.1.2, I just published 2.1.3

Okay, now it works, thanks again.