skip to Main Content

I created a page within a subdirectory called "Condominioo" and I need to add a link in the "NavMenu.razor" file so that when it is activated it calls the new page created.

enter image description here

When I click on the link, an error appears: Error: 404

2

Answers


  1. You should create Razor component files with the .razor extension, not .cshtml. The .cshtml extension is used for Razor Pages and MVC views in ASP.NET Core which are different from Blazor components

    To define a route in a .razor file, you use the @page directive at the top of the file. For example, if you have a MyPage.razor in the SomeFolder, you would start the file with @page "/path" or @page "/parentpath1/path".

    As per your example picture

    Index.razor

    @page "/condominioo/Index"
    
    <h3>My Custom Page</h3>
    <p>This is my custom page content.</p>
    

    NavMenu.razor

    <NavLink class="nav-link" href="/condominioo/Index">
        <span class="oi oi-home" aria-hidden="true"></span> My Custom Page
    </NavLink>
    

    Check Out this Small Tutorial it might help you a lot

    Login or Signup to reply.
  2. In modern Blazor apps to create a new page you should be using Razor Components (.razor) instead of the older MVC Razor Page/View `.cshtml’ – Classic Razor Pages…

    enter image description here

    In a Razor component we can use the @page directive to designate that component as a page and to define the route to access the page:

    @page "/index"
    
    <PageTitle>My New Page</PageTitle>
    
    <h1>Hello World</h1>
    

    It doesn’t matter what the physical path to that file is, I could put this in any subfolder of the project and still access it via the route defined in the directive.

    The NavLink to this would look like:

    <NavLink href="index">Index</NavLink>
    

    You can define complex routes if you want to, but the route you define is not tied to the physical location of the file, to make you existing nav link work, define your route as this:

    @page "/Condominioo/Index.cshtml"
    

    It doesn’t make a lot of sense to do that, but it will work 😉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search