What mgebhard did should work well. I guess you have applied paging in your application.And any other configurations would influence your routing.Please share more code which could reproduce your issue.
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
2 Points
10 Posts
url friendly in mvc core 3
Jul 04, 2020 08:19 AM|MLT111|LINK
hi
I have a url like below :
https://mysite.com/Product?id=2&title=test&page=1
and I want to change as follows :
https://mysite.com/2/test?page=1
My Route in mvc core 3.1 :
endpoints.MapControllerRoute( name: "Product", pattern: "{id}/{title}", defaults: new { controller = "Product", action = "Index"} );
But isn t working . plz hlep me .
All-Star
52971 Points
23571 Posts
Re: url friendly in mvc core 3
Jul 04, 2020 10:43 AM|mgebhard|LINK
Use route attributes on the Product controller.
Default route
Action
URL
Results
Route reference documentation
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.1
https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1
Member
2 Points
10 Posts
Re: url friendly in mvc core 3
Jul 04, 2020 01:11 PM|MLT111|LINK
thank you
But I need a special route for product .
endpoints.MapControllerRoute( name: "Product", pattern: "{id}/{title}", defaults: new { controller = "Product", action = "Index" } ); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}" );
All-Star
52971 Points
23571 Posts
Re: url friendly in mvc core 3
Jul 04, 2020 01:51 PM|mgebhard|LINK
Use an int constraint.
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "Product", pattern: "{id:int}/{title}", defaults: new { controller = "Product", action = "Index" }); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
Member
2 Points
10 Posts
Re: url friendly in mvc core 3
Jul 04, 2020 01:56 PM|MLT111|LINK
thank you
But is not working .
In page 2 , Routing can not work and switch to defualt route .
All-Star
52971 Points
23571 Posts
Re: url friendly in mvc core 3
Jul 04, 2020 02:30 PM|mgebhard|LINK
Works for me. I assume you have other issues with the code. I assume you are not providing the proper URL to route to the product controller.
I prefer this the route design.
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "Product", pattern: "Product/{id:int}/{title}", defaults: new { controller = "Product", action = "Index" }); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
Contributor
2690 Points
874 Posts
Re: url friendly in mvc core 3
Jul 06, 2020 07:34 AM|Rena Ni|LINK
Hi MLT111,
What mgebhard did should work well. I guess you have applied paging in your application.And any other configurations would influence your routing.Please share more code which could reproduce your issue.
Best Regards,
Rena