ASP.NET MVC 查看存储在数据存储中的信息
我正在寻找一些有关在数据存储(数据库、文件等)中存储视图并根据路由数据显示它们的建议,所有这些都使用 ASP.NET MVC 2 和 ASP.NET 路由。
例如,我希望能够根据以下路线数据显示不同的视图:
/{country}/
/{country}/{area}
但同样我希望显示:
/{planet}/
/{planet}/{satellite}
全部基于字符串,并且数据不固定。因此,基于段的数量,可能将其用作数据存储中的选择标准...此外,我可能不知道预先的段,因此它们都是动态的。
我希望我们能在这里收集一些不同的方法,作为所有人的参考 - 我确信有些方法并不适合所有人......
那么,你会怎么做呢?
Branislav Abadjimarinov 建议控制器工厂可用于动态查找和显示页面。我喜欢这个主意,你觉得怎么样?
I'm looking for some advice on storing views in a data-store (database, file, other) and display them based on routing data, all using ASP.NET MVC 2 and ASP.NET Routing.
For example, I'd like to be able to display different views based on the following route data:
/{country}/
/{country}/{area}
But in the same vein I'd like to display:
/{planet}/
/{planet}/{satellite}
All are based on strings, and the data isn't fixed. So based on the number of segments maybe, use that as the selection criteria into the data-store...additionally, I may not know the segments up front, so they'd all be dynamic.
I'm was hoping we could get a few different methods together here, as kind of a reference for all - I'm sure some methods won't suite everyone...
So, how would you do it?
Branislav Abadjimarinov suggested a Controller Factory which could be used to do the look-up and display the page dynamically. I like this idea, what do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVC 无法从该 url 中了解选择哪条路由。您必须使路线更加具体。例如:
/planet/{planet}/{satelite}
/country/{country}/{area}
您还可以选择定义自己的 控制器工厂。控制器工厂根据路由决定实例化哪个控制器。因此,您可以在其中添加一些自定义逻辑,例如 - 检查 {planet} 参数是否存在,如果存在则实例化 Planet 控制器,否则实例化 Nations 控制器。
There is no way for MVC to understand from this url's which route to choose. You have to make the routes more specific. For example:
/planet/{planet}/{satelite}
/country/{country}/{area}
You also have the option to define your own controller factory. The controller factory decides which controller to instantiate based on the route. So you can put some custom logic in it like - check if the {planet} parameter exist and if yes instantiate Planet controller else instantiate Countries controller.
这个帖子可以对你真的有帮助。
请记住,您始终可以添加新的路由规则:)
就像 这个
This Post could be really helpful for you.
Remember you always can add a new routing rule : )
Just like this