question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Route Attribute not working as expected on ComponentBase / Blazor

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I’m creating a simple component that inherits from a ComponentBase class, and my understanding is that I can use the Route attribute in the base class instead of the @page directive, unfortunately when I do that, I don’t get an error but the page is just blank

BLAZOR COMPONENT:

@inherits EmployeeListBase
<h3>Employee List</h3>

COMPONENT CLASS

using EmployeeManagement.Models;
using Microsoft.AspNetCore.Components;

namespace EmployeeManagement.Web.Pages
{
    [Route("/")]
    public class EmployeeListBase : ComponentBase
    {
        public IEnumerable<Employee> Employees { get; set; } = new List<Employee>();

    }
}

So basically, this should act as the root page of the razor web, instead, it just displays a blank page, no h3 text, nothing, just blank as follow

image

Expected Behavior

If I don’t use the Route attribute on the base class and use the @page directive in the component, then it work as expected:

image

According to the docs at https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-6.0 in the Routing section it states the following:

When a Razor file with an @page directive is compiled, the generated class is given a RouteAttribute specifying the route template.

Thank you so much for all your help! If I’m doing something wrong, PLEASE let me know, thank you.😃

Steps To Reproduce

No response

Exceptions (if any)

No exceptions, just a blank page is returned.

.NET Version

6

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
MariovanZeistcommented, Jan 6, 2022

Hi @sipi41. The [Route] attribute is defined as not inheritable so if you inherit from a base class that has a route attribute set, it won’t be honored on the descending class. This is because as @TanayParikh explained this would lead to ambiguity, as you can end up with multiple classes having the same route.

It’s perfectly fine to inherit from a base class, but you should add the @Page (in razor) or [Route] in .cs attribute only to the top-level component. (which you could call a Page, a Blazor component to which you can route to, like the /counter example on the https://blazor-university.com/components/ website

The Blazor routing code will ignore the [Route] attribute when set on a descending class, and that is what you are experiencing. Code here

1reaction
sipi41commented, Jan 6, 2022

@MariovanZeist Marvelously explained! this is why I love the way where all code is just there… it’s hard for us to remember that X thing is not transferable or inheritable… 😦 Thank you Mario! you are the hero of the week!

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net core - Blazor @page route url define with variable
The page directive gets compiled down to an attribute and has the same restrictions as C# attributes. You can use the @attribute with...
Read more >
ASP.NET Core Blazor dependency injection
Learn how Blazor apps can inject services into components. ... @inject (or the [Inject] attribute) isn't available for use in services.
Read more >
Working with Query Strings in Blazor
When a form was submitted the field names and their values were encoded into the URL as query string values. Why use them...
Read more >
Directives
This directive generates a [RouteAttribute] on the transpiled class ( [PageAttribute] in Blazor 3) , enabling Blazor routing (covered later) to identify which ......
Read more >
Learning Blazor Components: The Definitive Guide
In this guide, learn the fundamentals of Blazor Component construction and see how to build an example Blazor app that puts it all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found