开发者

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 wi开发者_Go百科th 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}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜