如何映射具有 3 个以上组件的 ASP.NET MVC 路由?

发布于 2024-11-08 04:13:55 字数 323 浏览 4 评论 0原文

我正在尝试学习 asp.net mvc,几乎在所有地方我都会看到包含三个组件的路由描述,例如 /Controller/Action/{anyParams} 我想知道是否可以映射类似于

/Folder(或命名空间)/Controller/Action/params... 的路线... 例如:

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

或者一般来说, /XYZ/Controller1/Action/{param}

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-11-15 04:13:55

是的,MapRoutes 函数中的第二个参数(通常在 Global.asax.cs 中是 Url,这可以是您想要的任何模式。类似于

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

应该可以解决问题。

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.

咿呀咿呀哟 2024-11-15 04:13:55

您可以根据需要将路线设置为复杂程度。

Fe 以下路由:

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

将路由到以下函数:

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

     }
}

因此您可以指定任意数量的参数,它们将作为函数参数传递到您的操作中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文