A
A
avikik2018-08-29 14:16:22
ASP.NET
avikik, 2018-08-29 14:16:22

How to do routing correctly?

Good day to all! Guys, please help.
I've been sitting with routing in my project for several days now, I can't set it up properly. I have already looked at many sources, but everywhere there are very simple examples and in some cases they help me, but in most cases I am powerless.
Maybe someone will throw off examples with routes, such as the definition hierarchy and how they need to be formatted themselves. I would be very grateful, below I will throw off my code. I work in MVC 5

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                null,
                url: "all",
                defaults: new { controller = "Home", action = "Index",page = 1}
                );

            routes.MapRoute(
                null,
                url: "all/{page}",
                defaults: new { controller = "Home", action = "Index",page = UrlParameter.Optional},
                constraints: new { page = @"\d+" }
                );

            routes.MapRoute(
                null,
                url: "news/{page}",
                defaults: new { controller = "Home", action = "News",},
                constraints: new { page = @"\d+" }
                );

            routes.MapRoute(
                null,
                url: "{category}",
                defaults: new { controller="Home",action= "Category",page = 1}
                );

            routes.MapRoute(
                null,
                url: "{category}/{page}",
                defaults: new { controller = "Home", action = "Category"}
                );

         
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );            
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question