aspnetcore: Router does not find inherited RouteAttribute
Since component is not a partial class the issue is that when i specify route (e.g. /interop) in base class and i won’t use @page in the view but use @inherits - i’ve got issue in console in the browser:
MonoPlatform.ts:70
Uncaught (in promise) Error: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidOperationException: The following routes are ambiguous:
'interop' in 'blazorfirststandaloneapplication.Base.InteropBase'
'interop' in 'blazor_first_standalone_application.Pages.Interop'
If it’s inheritance - in my opinion it should derive Route attribute.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (4 by maintainers)
Commits related to this issue
- Make RouteAttribute non-inherited Fixes: #5529 Inheriting and looking for inherited route attributes will cause nothing but trouble. We had a bug tracking what to do about this and we decided to mak... — committed to dotnet/aspnetcore by rynowak 5 years ago
- Make RouteAttribute non-inherited (#10236) * Make RouteAttribute non-inherited Fixes: #5529 Inheriting and looking for inherited route attributes will cause nothing but trouble. We had a bug t... — committed to dotnet/aspnetcore by rynowak 5 years ago
@SteveSandersonMS - I started taking a look at this because I was bored in a cool meeting. I started writing some code and tests, and realised that solving this in a satisfying way is not straightforward.
So the problem with the current state is that users want to specify a route attribute on a
.razorfile and then inherit that class from another class they write in C#. The problem is that we don’t give you a way to make a class as abstract in.razor- and it doesn’t seem like a generally useful thing. So if we enabled this feature the problem is that you’d end up with a duplicate. Both the base class and the derived class are routable with the same route.If you do things the other way, write your
.razorfile without@routeand put the route on your derived class then everything is fine even if we don’t do this feature.The confusing thing, is that this discussion seems to be oriented around people’s usage of inheritance to work around the fact that we don’t yet support partial classes. We plan to do that, so I’m not super motivated by these cases when we consider the long term behavior. Also, as discussed there’s currently a working recipe for the inheritance workaround.
SO what are the use cases for inheritance + routing? I struggle to think of any beyond this current workaround. So I guess we’d be left with our own arbitrary decision.
I think there are 3 things we could do here:
[Route]as non-inherited - this is the best option if we don’t like the other twoThe default for attributes is that they are inherited, but I think you could make the argument for routing that it makes sense NOT to be inherited because of these problems.
If we felt like there was some valuable scenario here, we’d consider doing 3. This would allow users to specify routes on a base class and inherit them and to subclass and replace those routes. We’d walk up the inheritance hierachy and stop at the first class with a route attribute.
Your thoughts?
Thoughts from the community? Does anyone have a meaningful scenario where they are not working around the lack of partial classes?
I tried the following and expected it to work like @rafek1241 says., with daily builds, it doesn’t work, but if I move the route to .cshtml it works fine. I don’t see any errors in the console, when it fails
the following in .cshtml