ASP.NET MVC route skips with format {ID} - {Slug}

I have a route like the following, ideally I would like it to match:

domain.com/layout/1-slug-is-the-name-of-the-page

        routes.MapRoute(
            "Layout",                                                // Route name
            "layout/{id}-{slug}",                                           // URL with parameters
            new { controller = "Home", action = "Index"}, new {id = @"\d+$"}
        );

But when I hit the url, I keep getting this exception:

The parameter dictionary contains a null entry for the parameter "id" of a non-zero type, "System.Int32" for the method "System.Web.Mvc.ActionResult Index (Int32)" in ...

The above route will correspond to the following:

domain.com/layout/1-slug or domain.com/layout/1-slug_permalink

It seems that the hyphen that separates the identifier from Slug is causing problems.

+3
source share
1 answer

URL . ( , slug). (, "\ d +" ) . , , , id , , , , , .

(, , ) idAndSlug, , id slug .

, mxmissile.

+6

Source: https://habr.com/ru/post/1739625/


All Articles