locked
url friendly in mvc core 3 RRS feed

  • Question

  • User-819032351 posted

    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 .

    Saturday, July 4, 2020 8:19 AM

All replies

  • User475983607 posted

    Use route attributes on the Product controller.

    Default route

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Product}/{action=Index}/{id?}");
    });

    Action

    public class ProductController : Controller
    {
        [Route("{id}/{title}")]
        public IActionResult Index(int id, string title, int page)
        {
            return Json(new { id = id, title = title, page = page });
        }
    }

    URL

    https://localhost:44301/2/test?page=1

    Results

    {
      "id": 2,
      "title": "test",
      "page": 1
    }

    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

    Saturday, July 4, 2020 10:43 AM
  • User-819032351 posted

    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}" );

    Saturday, July 4, 2020 1:11 PM
  • User475983607 posted

    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}" );

    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?}");
    });

    Saturday, July 4, 2020 1:51 PM
  • User-819032351 posted

    thank you

    But is not working .

    In page 2 , Routing can not work  and switch to defualt route .

    Saturday, July 4, 2020 1:56 PM
  • User475983607 posted

    In page 2 , Routing can not work  and switch to defualt route .

    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?}");
    });

    Saturday, July 4, 2020 2:30 PM
  • User711641945 posted

    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

    Monday, July 6, 2020 7:34 AM