Mvc route - using integer parameters

I have a route like this in mine global.asax.cs:

        routes.MapRoute(
           "NewsArticles",
           "News/{page}",
           new { controller = "News", action = "Index", archive = false }
       );

How can I restrict access to this route so that it occurs only if the user uses an integer?

+3
source share
1 answer

Make sure this route is listed before the default route. You can also use regular expressions to limit the possible parameter values:

routes.MapRoute(
    "NewsArticles",
    "News/{page}",
    new { controller = "News", action = "Index" },
    new { page = @"^\d{1,3}$" }
);

Note. In your example, you are using archive = falsewhile there is no parameter in the route archive.

+5
source

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


All Articles