Mat J
Mat J

Reputation: 5632

How can map an asp.net MVC route with more than 3 components?

I'm trying to learn asp.net mvc, and almost everywhere I see route description with three components like /Controller/Action/{anyParams} I'd like to know if I can map a route similar to,

/Folder(or namespace)/Controller/Action/params... ex:

    /Admin/Student/Edit/id
    /ABC/Faculty/Add/`
    /XYZ/Student/Edit/id

or in general, /XYZ/Controller1/Action/{param}

Upvotes: 0

Views: 252

Answers (2)

Jan Jongboom
Jan Jongboom

Reputation: 27352

You can make your routes as complex as you want.

F.e. the following route:

routes.MapRoute("some-route", "products/detail/order/{id}/{name}/",
             new { controller = "Products", action = "Order" },
             new { id = "^\d+" });

will route to the following function:

public class ProductsController : Controller {
     public ActionResult Order (int id, string name) {

     }
}

So you can specify as many parameters as you want, and they will be passed into your action as function parameters.

Upvotes: 1

vandatech
vandatech

Reputation: 26

Yep the second parameter in the MapRoutes function (usually in Global.asax.cs is Url and this can be any pattern you want. something like

routes.MapRoute("MyRoute", "XYZ/Controller1/Action/{param}", new {controller = "Controller1", action = "Action"}});

should do the trick.

Upvotes: 1

Related Questions