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.
When I click on the link, an error appears: Error: 404
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.
When I click on the link, an error appears: Error: 404
2
Answers
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 componentsTo define a route in a
.razor
file, you use the@page
directive at the top of the file. For example, if you have aMyPage.razor
in the SomeFolder, you would start the file with@page "/path"
or@page "/parentpath1/path"
.As per your example picture
Index.razor
NavMenu.razor
Check Out this Small Tutorial it might help you a lot
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…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: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:
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:
It doesn’t make a lot of sense to do that, but it will work 😉