Home > database >  How can i use classic MVC routing in razor pages?
How can i use classic MVC routing in razor pages?

Time:03-13

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

CodePudding user response:

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.

  • Related