如何映射具有 3 个以上组件的 ASP.NET MVC 路由?
我正在尝试学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,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.
您可以根据需要将路线设置为复杂程度。
Fe 以下路由:
将路由到以下函数:
因此您可以指定任意数量的参数,它们将作为函数参数传递到您的操作中。
You can make your routes as complex as you want.
F.e. the following route:
will route to the following function:
So you can specify as many parameters as you want, and they will be passed into your action as function parameters.