asp.net mvc 动态/相对路由
我正在开发一个具有模块化结构的网站。 URL 的每个部分都呈现一个内容项。 例如 url:www.mysite.com/blogs/programming/2010/01/
根项是“区域”类型的“博客”。它有一个类型为“博客”的子项目“编程”。
现在网址左侧有“2010/01”。 最后一个有效(可路由)项目“编程”是一个博客,因此我需要将“2010/01”映射到操作
BlogController.Date(int blogid, intyear,int?month,int?day)
每个控制器都来自一个模块(单独的模块) dll),它注册一些项目类型(博客注册类型“blog”(可路由)和“post”(不可路由)。“blog”可以有“post”类型的子项)。 当检测到 URL 的最后一个有效(可路由)项时,逻辑知道要查找哪个程序集和控制器。现在我需要一种方法来使用正确的参数调用正确的操作。
“博客”类型项目的一些路由
{year}/ {year}/{month} {year}/{month}/{day} feed/ category/{category-name}/ tag/{tag-name}/ search/{*phrase} {*post-name}
有什么建议吗?执行路由的简单方法是什么?
I'm developing a website which has a modular structure.
Every segment of the url presents an content item.
For example url: www.mysite.com/blogs/programming/2010/01/
Root item is 'blogs' of type 'area'. It has a child item 'programming' of type 'blog'.
Now there's '2010/01' left of the url.
Last valid (routable) item 'programming' was a blog so I need to map '2010/01' to action
BlogController.Date(int blogid, int year, int? month, int? day)
Every controller comes from a module (separate dll), which registers some item types (blog registers types 'blog' (routable) and 'post' (not routable). 'blog' can have children of type 'post').
When last valid (routable) item of the url is detected, logic knows which assembly and controller to look for. Now I need a way to invoke correct action with correct parameters.
Some routes for item of type 'blog'
{year}/ {year}/{month} {year}/{month}/{day} feed/ category/{category-name}/ tag/{tag-name}/ search/{*phrase} {*post-name}
Any suggestions what would be a simple way to do the routing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决操作参数签名问题,我个人会创建一个新的模型类“BlogModel”,并仅将其作为单个参数。这样,您将拥有一致的操作参数签名。然而,这需要更多的工作,因为您需要创建一个自定义 ModelBinder 对象“BlogModelBinder”并将其注册到 ModelBinderFactory(或在 MVC3 中的 DependencyResolver)。在“BlogModelBinder”中,您只需查找 RouteData 的参数和值并将其绑定到“BlogModel”中的相应字段即可。
根据我个人的经验,我认为没有一种简单的方法来注册您的路线:您仍然需要单独注册特定操作的路线网址。除非有人有一种有效的方法来注册路由 URL,否则您可以放心,因为我们都必须亲自参与管道代码。
To solve the action parameter signature problem, I personally would create a new Model class "BlogModel" and have only that as your single parameter. This way, you'd have a consistent action parameter signature. However, this would require a bit more work, as you would need to create a custom ModelBinder object "BlogModelBinder" and register it to the ModelBinderFactory (or in MVC3 the DependencyResolver). In the "BlogModelBinder" you simply look up the RouteData's parameters and values and bind it to the corresponding field in your "BlogModel."
From my personal experience, I don't think there's an easy way to register your routes: you still would have to individually register the route urls to a specific action. Unless someone has an efficient way of registering the route urls, you can take solace in knowing that we all have to get our hands dirty with the plumbing code.