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.
I don't agree with you. I think there are only two reasons why you want to use base class and inheritance:
- You want to have two separate files for a single component (
cshtmlandcs). In that case you can specify@pagein the view. Why do you want to do this incsfile? - You want to have a base class for a few derived components. In that case you have to specify
@pagein derived component because you cannot have 2 different components with the same route. Once again you can have@pagein the views.
Am I missing something?
No - you don't. Thank you for your answer. From what you have written I can assume that when I will add an [Route()] attribute to the component base class then this class will be excluded from inheritance. Is that correct?
Of course I know that sometimes we need to do that (when we want to prepare html template for this base class in the .cs file e.g. for generic tables or list views)
I'm sorry, but I don't understand what are you going to achieve.
I was reading https://learn-blazor.com/pages/dynamic-content/ and from that page I have found that we can create components with routes but only when it's code-only. I don't want to convince you to my opinion- I just want to understand why it is like that and not for example - some special class which derive from BlazorComponent and enables Route attribute because thanks to you I have found that it is working for now only in this way. Earlier I wanted to remove @page directives just to make my views more html-like. I wanted to achieve only one directive in the first line @inherits. Now I know that i can't achieve that.
Sorry, I have absolutely no experience with code only components.
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
[Route("/test")]
public class Testbase : BlazorComponent
{
}
the following in .cshtml
@inherits Testbase
this is a test
On the other hand, if the page has multiple levels of inheritance, it doesn't make sense to have the route attribute on the base classes, so to avoid all those cases, I guess it makes sense to have it in .cshtml only.
So you agree with me or not? I made new repository just to show you the problem. When you load this project you will see error inside developer tools console in WebBrowser. routing-problem-blazor
@rafek1241 As I said in my first post, I have expected it to work. the gist of what you have is the same as the code I posted above.
But consider this scenario. lets say, there is code only class which has a route specified on it, like the url you gave above and what if 2 pages like in your example(Counter1 and Counter2) inherit from that code only component and have Route attribute applied to them as well. should the router be routing to base class or the inherited class. how does routing work in this case?
Currently the RouteTable does not look for inherited RouteAttribute on components: https://github.com/aspnet/Blazor/blob/dev/src/Microsoft.AspNetCore.Blazor/Routing/RouteTable.cs#L26
This explains what @rafek1241 is observing. Flagging as a bug.
Current workaround is to do what you've noted above, which is to put the route information on the derived class (e.g., in the .cshtml file).
@SteveSandersonMS how would the routing work in the case I mentioned above?
@dlr1 I suppose that inherited RouteAttribute will be checked only if there is no attribute or @page directive in derived class. On the other hand if there are two derived components without RouteAttribute or @page directive we should probably get an exception at application startup. Currently when two components have the same url in @page directive we get a nice exception:
Error: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidOperationException: The following routes are ambiguous:
'counter' in 'Spa.Client.Pages.Counter2'
'counter' in 'Spa.Client.Pages.Counter'
We don't need to have Route attributes, if the components are not going to be routed, but used in other components
The workaround and solution for that (first):
- Remove allow for inheritance in the RouteAttribute.
- Create another class which derive from BlazorComponent and allow use RouteAttribute only for this class. (That is how we can create code-only components).
3.Modify compiler that blazor component layout
.cshtmlcan derive only from blazorComponent (not from the code-only class) Result: -Pros: We don't have [Route()] in blazor component so user can't setup route in the blazor component layout and blazor component class in the same time. -Cons: We don't have choose where we store our routing.
Second solution:
Change interpretation of this attribute in compiler. Attribute shouldn't be inherited or if it is - @page directive should be unnecessery when you derive from component class which are using RouteAttribute and we should be informed in IDE that base component class is trying to override our @page directive (as ERROR).
@SteveSandersonMS Now when defining a Route via the RouteAttribute in a baseClass (from which the pages are derived), the route exists twice and throws an exception because of the ambiguity.
Error: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidOperationException: The following routes are ambiguous:
'browseintegrationpoints' in 'Browser.Pages.Browse.BrowseBase'
'browseintegrationpoints' in 'Browser.Pages.Browse.Browse'
It's just
[Microsoft.AspNetCore.Blazor.Components.RouteAttribute("/browseintegrationpoints")]
public class BrowseBase : ComponentBase
{
No @page - Directive is present in the derived component's .cshtml
@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 .razor file 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 .razor file without @route and 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:
- Make
[Route]as non-inherited - this is the best option if we don't like the other two - Make route attributes inherit via the normal .NET mechanism
- Implement our own search logic.
The 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?
The 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.
I agree. Marking RouteAttribute as non-inherited does make sense. I don't see any obvious scenario where you'd want to declare routes on base classes and have them be inherited, since that immediately leads you into ambiguous route situations. This would be a very cheap fix, and arguably we should do it independently of the partial classes.
On the other hand, I also agree that partial classes are the actual solution to most people's problems here, so I don't consider this urgent.
Honestly I was just looking through the oldest bugs during a meeting and saw something we can resolve in like 30 seconds 😆.
I sometimes look through the "oldest bugs" in an area to make sure we're not letting something valuable languish.