自定义路由规则(例如www.app.com/project/35/search/89/edit/89)
我想创建如下路由规则:
www.app.com/project/35/search/89/edit/48 ---> 动作是在项目控制器中编辑
传递的变量应该是project#(35)、search#(89)和edit#(48)
有人可以帮我为此构建一个routes.MapRout()吗?
我想使用:
routes.MapRoute(
"Default",
"{controller}/{projectid}/search/{searchid}/{action}/{actionid}",
new { controller = "Home", action = "Edit", projectid = "", actionid = "" }
);
但从以前的经验来看,这种类型的 MapRoute 会失败...我只得到了以下格式的东西才能工作:
{controller}/{action}/{variable}
任何人都可以给我任何建议吗? 谢谢。
I would like to create routing rules like the following:
www.app.com/project/35/search/89/edit/48 ---> action is edit in the project controller
The passed variables should be project# (35), search# (89), and edit#(48)
Can someone help me structure a routes.MapRout() for this.
I want to use:
routes.MapRoute(
"Default",
"{controller}/{projectid}/search/{searchid}/{action}/{actionid}",
new { controller = "Home", action = "Edit", projectid = "", actionid = "" }
);
But from previous experience, this type of MapRoute will fail... I've only gotten something in the following format to work:
{controller}/{action}/{variable}
Can anyone give me any advice on this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老实说,听起来您需要使 URL 看起来像这样:
它会让您的喜欢变得更简单。
Honestly, it sounds like you need to make you URL's look like this:
It would make your like much simpler.
使用 ASP.NET MVC 对控制器进行分组
经常出现的一个问题是,在使用 ASP.NET MVC 构建大型应用程序时如何对控制器进行分组。 通常,问题被表述为 ASP.NET MVC 是否支持“区域”(Monorail 的一项功能)。 根据 Monorail 文档,
MonoRail 支持区域的概念,区域是控制器的逻辑组。 所有控制器都属于一个区域。 默认区域是一个空的(未命名的)区域。
虽然 ASP.NET MVC 中对此没有开箱即用的支持,但可扩展性模型允许构建非常接近的东西。
注册路线
我们要做的第一件事是调用我编写的两个新扩展方法来注册区域的路线。 此调用是在 Global.asax.cs 的 RegisterRoutes 方法中进行的。
路线.MapAreas("{控制器}/{动作}/{id}",
“区域演示”,
new[]{ "博客", "论坛" });
路线.MapRootArea("{控制器}/{动作}/{id}",
“区域演示”,
new {controller = "Home", action = "Index", id = "" });MapAreas 方法的第一个参数是您熟悉和喜爱的路由 URL 模式。 我们将在该 URL 前面添加一个区域。 第二个参数是根命名空间。 按照惯例,我们会将“.Areas.AreaName.Controllers”附加到提供的根命名空间,并将其用作查找控制器类型的命名空间。
http://haacked.com/archive/2008/11/ 04/areas-in-aspnetmvc.aspx
Grouping Controllers with ASP.NET MVC
A question that often comes up is how do you group controllers when building a large application with ASP.NET MVC. Often, the question is phrased as whether or not ASP.NET MVC supports “Areas”, a feature of Monorail. According to the Monorail documentation,
MonoRail supports the concept of areas, which are logical groups of controllers. All controllers belong to an area. The default area is an empty (unnamed) one
While there’s no out of the box support for this in ASP.NET MVC, the extensibility model allows building something pretty close.
Registering Routes
The first thing we do is call two new extension methods I wrote to register routes for the areas. This call is made in the RegisterRoutes method in Global.asax.cs.
routes.MapAreas("{controller}/{action}/{id}",
"AreasDemo",
new[]{ "Blogs", "Forums" });
routes.MapRootArea("{controller}/{action}/{id}",
"AreasDemo",
new { controller = "Home", action = "Index", id = "" });The first argument to the MapAreas method is the Routing URL pattern you know and love. We will prepend an area to that URL. The second argument is a root namespace. By convention, we will append “.Areas.AreaName.Controllers” to the provided root namespace and use that as the namespace in which we lookup controller types.
http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx
为什么这种格式对你来说失败了? 你所说的“失败”是什么意思? 如果它不起作用,请尝试将新的 MapRoute 放置在默认命令之上 - 路由集合以 FIFO 方式工作。
@Robert Harvey - 以 Basecamp 为例 - 他们在 URI 中使用非常相似的方法。 我认为这是比您建议的更干净、更好的方法。 一旦你掌握了路由的窍门,它甚至不会“更简单”。
Why did this format fail for you? What do you mean by "failed"? If it just didn't work, try placing your new MapRoute above the default commands - the Routes collection is working in a FIFO manner.
@Robert Harvey - Take a look at Basecamp for example - they are using a very similar approach in their URI's. I think it's a much cleaner and better approach than what you're suggesting. It's not even "Simpler" once you get the hang of routing.