Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add more routing to asp.net MVC Global.asax

I have one map route like :

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );



    }

but I want to add more route URL , how can I do that ?

like image 994
Nothing Avatar asked Sep 13 '11 01:09

Nothing


People also ask

How can add multiple route in ASP.NET MVC?

Multiple Routes You can also configure a custom route using the MapRoute extension method. You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

How do I register a route in global ASAX?

We register routes in the Global. asax file and we invoke a RegisterRoutes method in the Application_Start() method. Routing is used to create user friendly URLs. It can also be used to setup the startup page of the application, just like the ASP.NET Web Forms.

Can we add constraints to the route in MVC?

Attribute Routing is introduced in MVC 5.0. We can also define parameter constraints by placing a constraint name after the parameter name separated by a colon. There are many builtin routing constraints available. We can also create custom routing constraints.


1 Answers

Just add another MapRoute()

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

routes.MapRoute(
    "SecondRoute",
    "{controller}/{action}/{tags}",
    new { controller = "Products", action = "Index", tags = "" }
);

I suggest you go through this excellent post on routing by The Gu.

like image 113
Bala R Avatar answered Oct 06 '22 00:10

Bala R