[SOLVED] How can i use classic MVC routing in razor pages?

Issue

I have set the url in my categories page as

<a href="/Shared/product/@p.Id">

</a>

My product page physically exists in the Shared directory.

The page directive of the view is set as

@page "/product/{id}"

and the OnGet method as

public void OnGet(int id)
{
    
}

Why do i get an 404 error? How can i fix this one?
I would like to generate the url as /product/1 for example

I have tried setting the url as

<a href="/product/@p.Id">

</a>

but i get 404 also

Solution

You have overridden the default route to the page (https://www.learnrazorpages.com/razor-pages/routing#override-routes), which is generated from its file path (rooted in the Pages folder). Change the template to "{id}", representing only the route parameter. The file path (/shared/product) already matches the route that you want.

You should only override the route if you don’t want the URL to match the file path of the page.

Incidentally, the Shared folder in an MVC or Razor Pages app is intended only for partials and layout files, not pages.

Answered By – Mike Brind

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *